The explode()
string function can be used to seperate a string into an array. A limit parameter for the seperation is optional.
Syntax
With optional limit parameter:
$array = explode($boundary, $string, $limit);
Without optional limit parameter:
$array = explode($boundary, $string);
$array
The returned array on successful seperation, or FALSE
on failure. An empty array will be returned if $boundary
is not found in the $string
and the $limit
value is negative. See examples below.
$boundary (delimiter parameter)
Required parameter as string data type. It sets the boundary string that is used to specify where to seperate the $string
.
$string (string parameter)
Required parameter as string data type. It is the string to be seperated.
$limit (limit parameter)
Optional parameter as integer data type. Sets the maximum number of array elements to be returned. Without the optional limit parameter, an unlimited amount of array elements will be returned. See examples below.
Examples
Code snippets in examples below have been tested in PHP parser version 5.3.27.
String Seperation
Without optional limit parameter:
$array = explode($boundary, $string);
$string | $boundry | $array elements |
---|---|---|
"Ben, Sam, Edward, Joe" | "," | $array[0] => "Ben" |
"Ben, Sam, Edward, Joe" | ", " | $array[0] => "Ben" |
"Today is a sunny day." | " " | $array[0] => "Today" |
"Today is a sunny day." | "," | $array[0] => "Today is a sunny day." |
"80:20" | ":" | $array[0] => "80" |
"Limiter string 7 (seven)." | "7" | $array[0] => "Limiter string " |
"Limiter integer 7 (seven)." | 7 Not defined | $array[0] => "Limiter integer " |
With optional limit parameter:
$array = explode($boundary, $string, $limit);
$string | $boundry | $limit | $array elements |
---|---|---|---|
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | 3 | $array[0] => "Sue" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | 9 | $array[0] => "Sue" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | 0 | $array[0] => "Sue,Val,Ann,Pam,Isa,Rosa" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | -1 | $array[0] => "Sue" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | -3 | $array[0] => "Sue" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | -4 | $array[0] => "Sue" |
"Sue,Val,Ann,Pam,Isa,Rosa" | "," | -9 | Will return an empty array, e.g.$elements = count($array) will result to $elements == 0 |