explode() function introduction
The explode() function can split a string into an array.
Syntax: explode(separator,string,limit).
Parameters | Description |
---|---|
separator | Required. Specifies where to split the string. |
string | Required. The string to split. |
limit |
Optional. Specifies the number of array elements to be returned. Possible values:
|
This function returns an array composed of strings, each element of which is a substring separated by separator as a boundary point.
The separator parameter cannot be an empty string. If separator is the empty string (""), explode() returns FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element from string.
If the limit parameter is set, the returned array contains at most limit elements, and the last element will contain the remainder of the string.
If the limit parameter is negative, all but the last -limit elements are returned. This feature is new in PHP 5.1.0.
Program List: explode() example
<?php // Example $fruit = "Apple Banana Orange Lemon Mango Pear"; $fruitArray = explode(" ", $fruit); echo $fruitArray[]; // Apple echo $fruitArray[]; // Banana // Example $data = "gonn:*:nowamagic:::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); echo $user; // gonn echo $pass; // * ?>
Program execution result:
Apple
Banana
gonn
*
Program List: Example of explode() using limit parameter
<?php $str = 'one|two|three|four'; // positive limit print_r(explode('|', $str, )); // negative limit (since PHP .) print_r(explode('|', $str, -)); ?>
Program execution result:
Array ( [] => one [] => two|three|four ) Array ( [] => one [] => two [] => three )
Program List: Convert string into key value array
<?php // converts pure string into a trimmed keyed array function stringKeyedArray($string, $delimiter = ',', $kv = '=>') { if ($a = explode($delimiter, $string)) { // create parts foreach ($a as $s) { // each part if ($s) { if ($pos = strpos($s, $kv)) { // key/value delimiter $ka[trim(substr($s, , $pos))] = trim(substr($s, $pos + strlen($kv))); } else { // key delimiter not found $ka[] = trim($s); } } } return $ka; } } // stringKeyedArray $string = 'a=>, b=>, $a, c=>%, true, d=>ab c'; print_r(stringKeyedArray($string)); ?>
Program execution result:
Array
(
[a] =>
[b] =>
[] => $a
[c] => %
[] => true
[d] => ab c
)
PS: The difference between PHP function implode() and explode() function
The above content introduces you to the specific usage of the explode() function. When we encounter the PHP function implode(), it combines array elements into a string.
implode(separator,array)
separator optional. Specifies what is placed between array elements. Default is "" (empty string).
array required. Arrays to be combined into strings.
Although the separator parameter is optional. However, for backward compatibility, it is recommended that you use two parameters.
Example of PHP function implode()
<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); ?>
Output:
Hello World! Beautiful Day!
The above code example is a demonstration of the specific implementation function of the PHP function implode().