Split a string by a string
Syntax
explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array
Parameters
delimiter
The boundary string.
string
The input string.
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.
Note: Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
Return
Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter. If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
Examples
1 · delimiter · Comma
<? $delimiter = ","; $string = "word1,word2,word3,word4,word5"; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 [4] => word5 )
2 · delimiter · Newline
<? $delimiter = "\n"; $string = "word1\nword2\nword3\nword4\nword5"; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 [4] => word5 )
3 · delimiter · Space
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 [4] => word5 )
4 · delimiter · Empty
<? // returns false $delimiter = ""; $string = "word1 word2 word3 word4 word5"; $return = explode($delimiter, $string); print_r($return); ?>
5 · string · Empty
<? $delimiter = " "; $string = ""; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => )
6 · string · 1 delimiter
<? $delimiter = " "; $string = " "; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => [1] => )
7 · string · 1 Element
<? $delimiter = " "; $string = "word1"; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 )
8 · string · 1 Element 1 delimiter
<? $delimiter = " "; $string = "word1 "; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 [1] => )
9 · limit · -2
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = -2; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 )
10 · limit · -1
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = -1; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 )
11 · limit · 0
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = 0; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 word2 word3 word4 word5 )
12 · limit · 1
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = 1; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 word2 word3 word4 word5 )
13 · limit · 2
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = 2; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 [1] => word2 word3 word4 word5 )
14 · limit · PHP_INT_MAX
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $limit = PHP_INT_MAX; $return = explode($delimiter, $string, $limit); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 [4] => word5 )
15 · return · array
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $return = explode($delimiter, $string); print_r($return); ?>
Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word4 [4] => word5 )
16 · return · array index
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; $return = explode($delimiter, $string); echo $return[0] . PHP_EOL; echo $return[1] . PHP_EOL; echo $return[2] . PHP_EOL; echo $return[3] . PHP_EOL; echo $return[4]; ?>
word1 word2 word3 word4 word5
17 · return · list variable
<? $delimiter = " "; $string = "word1 word2 word3 word4 word5"; list($var1, $var2, $var3, $var4, $var5) = explode($delimiter, $string); echo $var1 . PHP_EOL; echo $var2 . PHP_EOL; echo $var3 . PHP_EOL; echo $var4 . PHP_EOL; echo $var5; ?>
word1 word2 word3 word4 word5