What is the difference between several applications of the PHP explode () function and the implode () function, Explodeimplode
Explode () function introduction
The explode () function splits a string into arrays.
Syntax: Explode (separator,string,limit).
| parameter |
description |
| Separator |
Necessary. Specifies where to split the string. |
| String |
Necessary. The string to split. |
| Limit |
Optional. Specifies the number of array elements that are returned. Possible values:
- Greater than 0-Returns an array containing a maximum of 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 separator contains a value that is not found in a string, explode () returns an array containing a single element in the string.
If the limit parameter is set, the returned array contains a maximum of 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 PHP 5.1.0.
Program List:explode () example
<?php//Example $fruit = "Apple Banana Orange Lemon Mango Pear"; $fruitArray = Explode ("", $fruit); Echo $fruitArray []; Appleecho $fruitArray []; banana//Example $data = "gonn:*:nowamagic:::/home/foo:/bin/sh"; list ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data); Echo $user; Gonnecho $pass; *?>
Program Run Result:
Apple
Banana
Gonn
*
Program List: Explode () example using the limit parameter
<?php$str = ' one|two|three|four ';//Positive Limitprint_r (Explode (' | ', $STR,));//negative limit (since PHP.) Print_r (Explode (' | ', $STR,-));? >
Program Run Result:
Array ( [] = = One [] = two|three|four) Array ( [] = = One [] = [] = [] = three)
Program List: Converts a string into an array of key values
<?php//converts pure string into a trimmed keyed arrayfunction stringkeyedarray ($string, $delimiter = ', ', $kv = ' => ;') {if ($a = explode ($delimiter, $string)) {//Create parts foreach ($a as $s) {//Per part if ($s) { if ($po s = 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 ';p rint_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 gives you a description of the explode () function of the specific use. When we encounter the PHP function implode () combines the array elements into a string.
Implode (Separator,array)
Separator is optional. Specifies what is placed between the elements of the array. The default is "" (an empty string).
Array required. An array to combine as a string.
Although the separator parameter is optional. However, for backwards compatibility, it is recommended that you use two parameters.
Example of PHP function implode ()
Output:
Hello world! Beautiful day!
The above code example is the implementation function of PHP function implode ().
http://www.bkjia.com/PHPjc/1068833.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068833.html techarticle There are several applications of the PHP explode () function and the implode () function, the explodeimplode explode () function introduces the explode () function to divide the string into arrays. Syntax: Explode (Sep ...