Introduction to Explode () function
The explode () function splits the string into arrays.
Syntax: Explode (separator,string,limit).
Parameters |
Description |
Separator |
Necessary. Specify where to split the string. |
String |
Necessary. The string to split. |
Limit |
Optional. Specify the number of array elements returned. Possible values:
- Greater than 0-Returns an array containing up to limit elements
- Less than 0-returns an array containing all elements except the last-limit element
- 0-Returns an array containing an element
|
This function returns an array of strings, each of which is a substring separated by separator as a boundary point.
The separator parameter cannot be an empty string. If separator is an empty string (""), Explode () returns FALSE. If the value contained in separator is not found in string, explode () returns an array that contains a single element in string.
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 the limit parameter is a negative number, all elements except the last-limit element are returned. This feature is new in the 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 Run Result:
Apple
Banana
Gonn
*
Program List: Explode () example using the limit parameter
<?php
$str = ' one|two|three|four ';
Positive limit
print_r (Explode (' | ', $STR,));
Negative limit (since PHP.)
Print_r (Explode (' | ', $STR,-));
? >
Program Run Result:
Array
(
[] => one
[] => two|three|four
)
array
(
[] => one
[] => two
[] => three
)
Program List: A string into an array of key values
<?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 + Strl En ($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 Run Result:
Array
(
[A] =>
[b] =>
[] => $a
[C] =>%
[] => True
[d] => AB C
)
The difference between the ps:php function implode () and the Explode () function
The above content introduces the specific usage of the explode () function. When we encounter the PHP function implode () combines the array elements into a single string.
Implode (Separator,array)
Separator optional. Specify what is placed between array elements. The default is "" (empty string).
Array required. The array to combine as a string.
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 the implementation function of PHP function implode ().