Copy code The code is as follows:
$array=explode(separator,$string);
$string=implode (glue,$array);
The key to using and understanding these two functions is the relationship between separator and glue. When converting an array to a string, glue characters - characters or codes that will be inserted between the array values in the resulting string - are set.
In contrast, when converting a string to an array, you specify a delimiter, which is used to mark what should become individual array elements. For example, start with a string:
$s1='Mon-Tue-Wed-Thu-Fri';
$days_array=explode('-',$s1);
The $days_array variable is now an array with 5 elements, Its element Mon has index 0, Tue has index 1, and so on.
$s2=implode(',',$days_array);
$s2
The variable is now a comma-separated list of days of the week: Mon, Tue, Wed, Thu, Fri
Example 1. explode() example
Copy code The code is as follows:
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces [0]; // piece1
echo $pieces[1]; // piece2
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin /sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
Example 2. limit parameter example
Copy code The code is as follows:
$str = 'one|two|three|four';
/ / Positive limit
print_r(explode('|', $str, 2));
// Negative limit
print_r(explode('|', $str, -1));
?>
The above example will output:
Array
(
[0] => one
[1] => two|three |four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Note: This function can be safely used on binary objects.
http://www.bkjia.com/PHPjc/326701.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326701.htmlTechArticleCopy the code The code is as follows: $array=explode(separator,$string); $string=implode(glue,$ array); The key to using and understanding these two functions is the separator and the glue character (g...