PHP explode() – split a string by string into array

PHP explode() function is used for split a string by another string into an array. This function is used when we need to split a string using a specific character or string present in that string.
Its Syntax will be :
explode (delimiter string , string to explode , LIMIT );

Here delimiter string will declare at which string occurrence the split process should happen . And LIMIT is an optional parameter for specifying the maximum count of splitting. If we specify LIMIT parameter , maximum number of splitting will be the LIMIT value and the rest of string will be the last element in array.

We will get more clear picture about explode() function after seeing some examples.

Sample PHP program using explode() function

<?php
$data="How to split a string using explode";
$splittedstring=explode(" ",$data);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = How
splittedstring[1] = to
splittedstring[2] = split
splittedstring[3] = a
splittedstring[4] = string
splittedstring[5] = using
splittedstring[6] = explode

Here , we are trying to split the string at blank spaces with out optional parameter LIMIT value. And we can see that string is split at the positions where blank space occurred and stored in the array $splittedstring. We can retrieve the values splittedstring[0] for first split string and splittedstring[1] for the second part etc..
Here in this example I am looping through the array and output all the results.

Now look at a sample program with optional parameter LIMIT count with explode()

<?php
$data="How to split a string using explode";
$splittedstring=explode(" ",$data,3);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = How
splittedstring[1] = to
splittedstring[2] = split a string using explode

Here we can see that optional parameter is specified as 3 as limit count. So the string is split to three parts and last part contain the rest of the original string.

Now look at the next sample PHP program with explode() function

<?php
$data="http://www.youtube.com/watch?v=ZR0iBvof3MA";
$splittedstring=explode("?v",$data);
echo "Video ID is ".$splittedstring[1];
?>

Output will be :

Video ID is =ZR0iBvof3MA

Here we trying to split a youtube video url by a string ?v for getting its video id.

Optional parameter with negative value:
If we specified -LIMIT , then the last LIMIT number of splitted results wont return.
See the flowing example and its output for understand whats happening

<?php
$data="ONE TWO THREE FOUR FIVE";
$splittedstring=explode(" ",$data,-2);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = ONE
splittedstring[1] = TWO
splittedstring[2] = THREE

Here we can see that it wont store the last two components ( FOUR and FIVE ) in array.

Exit mobile version