Explode and implode in PHP, ballet shoes, look forward to the Huubang to help!
Speaking of PHP, the function is often the main, is also PHP enthusiasts, and like PHP advanced friends must control the east, learning PHP Huubang also know that the array is also necessary to grasp, can that way, advanced PHP, Also is to learn the array in PHP and in the array is usually used to divide the string ah what, it will be used to explode, and implode function, the next said in PHP explode and implode usage, ballet shoes, hope for the Huubang have some assistance!
Usage of explode in PHP
(PHP 3, PHP 4, PHP 5) explode--use a string to split another string description
Array explode (string separator, string string [, int limit])
The function goes to an array of strings, each of which is a substring of string, separated by the string separator as the boundary polygon.
If the limit parameter is set, the array to be returned includes a minimum of limit elements, and the last element will contain the remainder of the string.
If separator is an empty string (""), explode () will go to FALSE.
If the value included in the separator is not found in the string, then explode () will go to the array containing string two-element meat.
If the limit parameter is a negative number, go to all the meta-meat except the last limit element. Pete Sex is new in PHP 5.1.0.
For historical reasons, although implode () can accept two kinds of parameter order, but explode () does not stop. You must ensure that the separator parameter is not preceded by a string parameter.
Note: The parameter limit is participating in PHP 4.0.1.
Example 1.
Explode () example
The code is as follows:
<?php///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.
Example of the limit parameter
The code is as follows:
<?php $str = ' one|two|three|four '; Positive limit Print_r (Explode (' | ', $STR, 2)); Negative limit Print_r (Explode (' | ', $STR,-1));?>
The above example outputs: Array ([0] = one [1] = Two|three|four) Array ([0] = one [1] = [2] = three)
Note: When the function can be used safely both into the building for the elephant.
PHP implode () function
Example <?php$arr = Array (' Hello ', ' world! ', ' Beautiful ', ' day! '); echo Implode ("", $arr);? >
Output:
Hello world! Beautiful day!
The implode () function combines array elements into a single string.
Grammar implode (Separator,array)
Parameter description Separator Optional. Delimits the contents of put aside between array elements. Acquiescence is "" (empty string). Array must be, 400 telephones. An array to federate as a string. Clarified that although the separator parameter is optional. But for the sake of compatibility, the use of two parameters is recommended.
Reminders and body text: implode () can accept two types of parameter order. But because of historical reasons, explode () is not stopped. You must ensure that the separator parameter is not stopped until the string parameter.
PHP Explode and implode
The code is as follows:
<?php $zongzi = "1|2|3|4|5|6"; $zongzi = Explode ("|", $zongzi); Var_dump ($zongzi); $zongzi = Implode ("|", $zongzi); echo $zongzi;?>