This is a detailed tutorial of the PHP explode() function. Learn to split a string into an array using any delimiter with the help of illustrative examples.
Table of Contents
PHP explode() Function
The function explode()
comes built-in with PHP version 4 and above. This function is used to splits a given string into pieces according to the provided delimiter and then return an array in which those split pieces of the strings are put into as the elements of the array. In other words, this function converts a string into an array by splitting it from delimiters. It is a very helpful function and highly used in different aspects of practical applications that are built using PHP.
Syntax
1 |
$stringArray = explode($delimeter, $string, $limit); |
Parameters
The explode()
function can take upto three arguments. But only two arguments are actually required, the third one is optional to be provided.
The first parameter is the $delimiter
. This can be a symbol, a character or even a string itself, from where you wanted to split the string. For example, if you want to split a string at the places, where it encountered the symbol semi-colon (;), then the symbol semi-colon is the delimiter here.
The second parameter is the $string
. This is the actual string that you want to split into pieces as the elements of an array.
The third, last and the optional parameter is $limit
. It is basically an integer parameter that defines the number of elements that you want to keep in the resultant array. In other words, how many instances of the delimiters you wanted in the string to be split. The string will be split only from the first $limit
number of occurrences.
- If the
$limit
is a positive integer, then the string will split whenever it encounters this much number of occurrences of the delimiter from the left. - If the
$limit
is a negative integer, then the string will split whenever it encounters this much number of occurrences of the delimiter from the right. - If the
$limit
is 0 (Zero), then the resultant array will contain the provided string as it is, without any kind of splitting.
Return Type
This function will return an array of the strings, which are there in the array as a result of the splitting process performed on the main provided string according to the delimiter locations.
Example
We have a taken a string which basically contains different sports names separated by commas. Now, we’ll make use of the explode function in different ways to generate a new array containing these different sports as the elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $sportsString = "Basketball, Cricket, Badminton, Tennis, Volleyball"; #Simple Usage $sportsArray = explode(",", $sportsString); print_r($sportsArray); #Split only from First 3 Delimeter Occurances $sportsArray = explode(",", $sportsString, 3); print_r($sportsArray); #Split only from Last 3 Delimeter Occurances $sportsArray = explode(",", $sportsString, -3); print_r($sportsArray); #Do not split anything $sportsArray = explode(",", $sportsString, 0); print_r($sportsArray); ?> |
Make sure you carefully observe the output of this program to better understand how the PHP explode() function works and splits the given string into array elements.
I hope you found this guide useful. If so, do share it with others who are willing to learn different programming skills in PHP and other IT Technologies. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!