Purpose of explode function
- The explode function, as the function name suggests, is used to split a string to the given number of substrings in PHP.
- It returns an array of broken substrings that you may assign to an array variable.
- You may find the details of the explode function in the last part of this tutorial, first, have a look a the syntax and live examples of using the explode PHP function.
Syntax of using the PHP explode
This is how you can use the explode function of PHP:
An explode example to split phone numbers
In the following example, we have declared a string variable and assigned it a phone number in this format:
After that, the explode function is used to split a string by using the hyphen (dash) delimiter. The returned array is assigned to an array.
Finally, we used a PHP foreach loop to display the array element values that are split substrings of the phone number string.
As there is no limit parameter, the whole phone number will be broken into three substrings.
See and example and output below:
$phone_number = "001-234-567678"; //Using the explode function $arr_ph = explode("-",$phone_number); //foreach loop to display the returned array foreach($arr_ph as $i){ echo $i . "<br />"; }
Output:
234
567678
An example with space delimiter
In this example, we used the space delimiter. An example string is created as below:
This is explode tutorial that enables string split in PHP
- Again, we did not use the limit parameter in that example.
- After creating the string, the PHP explode function is used to split that string.
- A foreach loop to display the returned array elements is used.
- Finally, we used the count function to display the total number of substrings or elements in that array.
See the example and output:
$gen_string = "This is explode tutorial that enables string split in PHP"; //Using the explode function $arr_string = explode(" ",$gen_string); //foreach loop to display the returned array foreach($arr_string as $str){ echo $str . "<br />"; } echo "Total number of substrings is: " . count($arr_string);
Output:
is
explode
tutorial
that
enables
string
split
in
PHP
Total number of substrings is: 10
An example with the limit parameter
This is the same example as above except we used the limit parameter to specify the number of splits in the string. As such the string we used is:
“This is explode tutorial that enables string split in PHP”
See the example and output online:
$gen_string = "This is explode tutorial that enables string split in PHP"; //Using the explode function $arr_string = explode(" ",$gen_string, 5); //foreach loop to display the returned array foreach($arr_string as $str){ echo $str . "<br />"; } echo "Total number of substrings is: " . count($arr_string);
Output:
is
explode
tutorial
that enables string split in PHP
Total number of substrings is: 5
You can see that only five substrings are displayed while the last substring is the remaining substring of the given string.
The total count of the array element is also 5 rather than 10 as in the above example.
Why do we use explode function?
The explode function is also searched as “PHP split” or split a string, which is used to split the given strings. For example, your web form is taking the phone number with country and area code in this format:
001-234-567678
While you need to separate the country and area codes that are separated by dashes.
In that case, you can use the explode PHP function to break that phone number by using dash or hyphen as a delimiter, after taking this as input in string format.
The explode function will break this into the following array elements since explode returns an array of broken strings or substrings.
The First item = 001
The Second item = 234
The Third item = 567678
How to use explode function of PHP
As shown in the first part of this guide, you may specify three parameters in the PHP explode function. Out of those, the last is an optional parameter i.e. specifying the number of splits or limiting the string pieces.
See the following part for a description of each parameter:
$array_name = explode ($delimiter, $string_to_split, $no_of_breaks)
Parameter | Description |
$array_name | The $array_name is an array; as such explode function returns an array of split strings. |
$delimiter | The $delimiter specifies the boundary string. Like in the above example, we used “–“ as a delimiter to break the phone numbers.
Similarly, you can use space, commas, or any other words as a $delimiter. |
$string_to_split | The next parameter is the string that you want to split by PHP explode function.
This can be a string variable, a sentence, etc. |
$no_of_breaks | The last parameter specified is the limit or number of breaks in the given string.
This is an optional parameter. |
You may also use a negative value. In that case, all substrings except the last one are returned.
As the explode function returns an array of the substrings, you can assign it to an array. After using the explode function, you can use foreach loop to go through the array elements, as shown in the above examples.