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

The use of explode and implode in PHP

angryTom
angryTomforward
2019-10-15 14:14:192841browse

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));

Result:

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

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);

implode

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

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!

Statement:
This article is reproduced at:blog.csdn.net. If there is any infringement, please contact admin@php.cn delete