Syntax:
explode(‘delimiter’,’string’,[limit])
Here, the first two parameters are mandatory parameters and the last parameter is optional. The searching string will be defined in the first parameter and the main string which will be split will be defined in the second parameter. Any numeric value can be set as optional parameter which will set the limit of splitting.
Example-1: Using empty string delimiter
In the following example, a single space is used as delimiter which will split $mystr string into three words. Here, $parts is the array of three elements which contains the return values of explode() function. Save the code in a php file and store it on web server location.
$mystr=‘I like Programming’;
//space is used as delimiter
$parts=explode(‘ ‘, $mystr);
echo $parts[0] . ‘<br/>’;
echo $parts[1] . ‘<br/>’;
echo $parts[2];
?>
Output:
When you will run the code from localhost then the following output will appear.
Example-2: Using particular character as delimiter
Without space, any character or string can be used as delimiter. In the following code, ‘easy’ is used as delimiter. After using explode, the string will be divided into two parts and $parts array will contain two elements which are printed later.
Output:
When you will run the code from localhost then the following output will appear.
Example-3: using list function to read the value of returning array
list is another built-in function of PHP which reads values of any array into variables. In the following example, ‘;’ is used as delimiter and the return values of explode function are retrieved by three variables named $a, $b and $c using list function.
Output:
When you will run the code from localhost then the following output will appear.
Example-4: Using positive limit parameter
explode() function accepts positive or negative number as third parameter. Using positive limit value in explode() function, the number of splitting can be reduced. If you explode the following string without limit then it will create an array of 6 elements. 5 is used as limit value in the following example. For this, the last elements of the array will be created by combining last two months.
Output:
When you will run the code from localhost then the following output will appear.
Example-5: Using negative limit parameter
In the following example, negative values is used as limit. When you assign any negative value in a function then it counts from the last part of the string or array. For the value -2, the last 2 weekday name will not be added as array elements.
$mystr = ‘Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday|Friday’;
//set negative value as limit
$output=explode(‘|’,$mystr,-2);
//Count total number of array elements
$length=count($output);
print_r($output);
echo ‘<br/>Total elements of the array is: ‘ . $length;
//Print the last element of the array
echo ‘<br/>Last element of the array is: ‘ . $output[$length–1] . ‘<br/>’;
?>
Output:
When you will run the code from localhost then the following output will appear.
The concept of explode function will be cleared after practicing the above examples properly. There is another built-in function of PHP named implode which is the opposite of the explode function.