Home > Backend Development > PHP Tutorial > The use of explode and implode in PHP

The use of explode and implode in PHP

angryTom
Release: 2023-04-07 16:32:02
forward
3126 people have browsed it

The use of explode and implode in PHP

##explode() function splits the string into arrays;

implode() function splits the array into The elements are combined into a string.

explode

Define array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

Parameters: limit limits the number of results.

$str = "hi, jason, world";
print_r(explode(',', $str));
print_r(explode(',', $str, 2));
Copy after login

Result:

Array
(
[0] => hi
[1] => jason
[2] => world
)
Array
(
[0] => hi
[1] => jason, world
)
Copy after login

Exercise example: Define a string a, calculate how much the values ​​after the underscore add up, and use the split string function.

 $a='331612_1,331495_1,331461_2,331559_1,333080_1,333555_1,331448_1,331618_1,333388_1,33
 3408_1,327937_1,331615_1,331499_1';
 $arr=explode(',',$a);
 $sum=0;
 foreach ($arr as $value) {
     $ary=explode('_', $value);
     $sum+=$ary[1];
 }
 print_r($sum);
Copy after login

implode

Definition string implode ( string $glue , array $pieces )

<?php
$array  = array( &#39;lastname&#39; ,  &#39;email&#39; ,  &#39;phone&#39; );
 $comma_separated  =  implode ( "," ,  $array );
echo  $comma_separated ;  // lastname,email,phone
Copy after login

For more PHP related knowledge, please visit

PHP Chinese website!

The above is the detailed content of The use of explode and implode in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:blog.csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template