Explode () separates one string from another.

array explode( string $delimiter , string $string [, int $limit ])
Copy the code

Parameters to describe

delimiter

The delimiter character on the boundary.

string

The input string.

limit

If the limit argument is set and is positive, the returned array contains maximum limit elements, and the last element will contain the rest of the string.

If the limit argument is negative, all elements except the last -limit element are returned.

If the limit is 0, it will be treated as 1.

For historical reasons, implode() can accept two arguments, but explode() cannot. You must ensure that the separator argument precedes the string argument.

The return value:

This function returns an array of strings, each element a substring of string, separated by the string Delimiter as a boundary point.

If delimiter is an empty string (“”), explode() returns FALSE. If delimiter contains a value that is not found in string and limit is a negative number, an empty array is returned, otherwise an array containing a single element of string is returned.

Example 1:

<? $pizza ="piece1 piece2 piece3 piece4 piece5 piece6"; $pieces =explode(" ", $pizza); echo $pieces[0]; // piece1echo $pieces[1]; / / piece2 / / sample 2 $data = "foo: * : : 1023-1000: / home/foo: / bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell)=explode(":", $data); echo $user; // foo echo $pass; / / *? >Copy the code

Example 2:

<? php $input1 ="hello"; $input2 ="hello,there"; var_dump(explode(',', $input1 )); var_dump(explode(',', $input2 )); ? >Copy the code

Output:

array(1)
(
    [0]=>string(5)"hello"
)

array(2)
(
    [0]=>string(5)"hello"
    [1]=>string(5)"there"
)
Copy the code

Example 3:

<? php $str ='one|two|three|four'; / / positive limit print_r (explodes (' | '$STR, 2)); / / negative limit (since PHP 5.1) print_r (explodes (' | '$STR, 1)); ? >Copy the code

Output:

Array(
    [0]=> one    
    [1]=> two|three|four
)

Array(
    [0]=> one  
    [1]=> two    
    [2]=> three
)
Copy the code