Member Login:


PHP: Split String Into An Array

Document Reference: TN200910002 - Rev: 4.11 - Last Update: 06-Apr-2014 23:53 GMT - Downloaded: 24-Jul-2019 14:41 GMT

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"
$array[1] => " Sam"
$array[2] => " Edward"
$array[3] => " Joe"
"Ben, Sam, Edward, Joe"", "$array[0] => "Ben"
$array[1] => "Sam"
$array[2] => "Edward"
$array[3] => "Joe"
"Today is a sunny day."" "$array[0] => "Today"
$array[1] => "is"
$array[2] => "a"
$array[3] => "sunny"
$array[4] => "day."
"Today is a sunny day."","$array[0] => "Today is a sunny day."
"80:20"":"$array[0] => "80"
$array[1] => "20"
"Limiter string 7 (seven).""7"$array[0] => "Limiter string "
$array[1] => " (seven)."
"Limiter integer 7 (seven)."7
Not defined
$array[0] => "Limiter integer "
$array[1] => " (seven)."
With optional limit parameter:
$array = explode($boundary, $string, $limit);
$string$boundry$limit$array elements
"Sue,Val,Ann,Pam,Isa,Rosa"","3$array[0] => "Sue"
$array[1] => "Val"
$array[2] => "Ann,Pam,Isa,Rosa"
"Sue,Val,Ann,Pam,Isa,Rosa"","9$array[0] => "Sue"
$array[1] => "Val"
$array[2] => "Ann"
$array[3] => "Pam"
$array[4] => "Isa"
$array[5] => "Rosa"
"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"
$array[1] => "Val"
$array[2] => "Ann"
$array[3] => "Pam"
$array[4] => "Isa"
"Sue,Val,Ann,Pam,Isa,Rosa"","-3$array[0] => "Sue"
$array[1] => "Val"
$array[2] => "Ann"
"Sue,Val,Ann,Pam,Isa,Rosa"","-4$array[0] => "Sue"
$array[1] => "Val"
"Sue,Val,Ann,Pam,Isa,Rosa"","-9Will return an empty array, e.g.
$elements = count($array)
will result to $elements == 0

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message