1. PHP String segmentation function explode ()
Explode () Usage:
Array explode (string separator,string string [, int limit])
Explode () Description:
This function returns an array of strings, each of which is a substring of string, separated by the separator of the strings as the boundary points.
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 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 of string.
If the limit parameter is a negative number, all elements except the last limit element are returned.
Explode () example
<?php//example 1$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6″; $pieces = Explode (" ", $pizza); Echo $pieces [0]; Piece1echo $pieces [1]; Piece2?>
<?php//example 2$data = "foo:*:1023:1000::/home/foo:/bin/sh"; list ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explod E (":", $data); Echo $user; Fooecho $pass; *?>
<?php//limit parameter Example $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: This function can be used safely with binary objects.
2. PHP string Merge function implode ()
Implode () Usage:
Array implode (string separator,string array)
Implode () Description:
The implode () function combines array elements into a single string.
Separator is optional. Specifies what is placed between the elements of the array. The default is "" (an empty string).
Array required.
Implode () Example:
<?php$arr = Array (' Hello ', ' world! ', ' Beautiful ', ' day! '); echo Implode ("", $arr);? >
Output:
Hello world! Beautiful day!
Note: Implode () can receive two parameter sequences, but explode () does not, you must ensure that the separator parameter precedes the string parameter.