Home > Article > Backend Development > php, explode php separate string into array
explode
explode — Use one string to split another string
array explode ( string $separator , string $string [, int $limit ] )
This function returns an array composed of strings, each element is A substring of string, which is separated by string separator as a boundary point. If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the remainder of the string.
If separator is an empty string (""), explode() will return FALSE. If separator contains a value not found in string, explode() returns an array containing a single element of string.
If the limit parameter is negative, all elements except the last -limit elements are returned. This feature is new in PHP 5.1.0.
Due to historical reasons, although implode() can receive two parameter orders, explode() cannot. You must ensure that the separator parameter comes before the string parameter.
Note: When constructing the sql statement, the query column can be written like this
Php code
$field = explode( ':','*');// I only learned about the usage of explode today
Official demo :
Php code
<?php // 示例 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 // 示例 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); echo $user; // foo echo $pass; // * ?>