Skip to main content

PHP Explode Function to Split String Into Array

One of the most popular string operation is breaking a string into an array on the basis of any defined delimiter. PHP provides an inbuilt function for splitting or breaking the string into the array. In this post, I will take you through the PHP explode function in detail. First, we will see the basic structure of PHP explode function and then we will explore some real-time usage.

PHP Explode Function

PHP explode function accepts 3 parameters which i.e. a delimiter, a string and a limit and returns an array.

Signature of PHP Explode Function

explode(Delimiter, String, Limit);

List of Parameters of PHP explode function

ParameterDescription
DelimeterA String type and mandatory parameter on the basis of which the main string will be split. It could either be a single character or a word. Mainly we call it as boundary string
StringThe main string which will be split on the basis of delimiter. Of course mandatory parameter
LimitA non-mandatory integer. If it is passed as a positive number then php explode function will split only up to the limit number passed., In the case of the negative number the all split component string will be returned except the limit number. If zero will be passed then it will be treated as 1 and only one split will be performed. If you do not pass the limit then the complete split will happen.

Return from Explode Function

  1. If you will only pass delimiter and string then explode function will return an array of the string which will be split on the basis of delimiter.
  2. In case you will pass delimiter as an empty string then php explode function will return false also your script will throw warning.
  3. If limit will be passed as a positive number along with delimiter then split will happen for number defined in the limit parameter and the last index of the array will contain rest of the string in which explode was not performed.
  4. If the delimiter is passed which do not exists in the string then explode function will return an array and the first index will contain the complete string.

Basic Example of PHP Explode Function

A basic explode function example where string will be exploded on the basis of delimiter space:
<?php
$string = 'This is just another test of php explode function';
$delimiter = ' ';
$result_array = explode($delimiter, $string);
var_dump($result_array);
?>

Output will be:

  0 => string 'This' (length=4)
  1 => string 'is' (length=2)
  2 => string 'just' (length=4)
  3 => string 'another' (length=7)
  4 => string 'test' (length=4)
  5 => string 'of' (length=2)
  6 => string 'php' (length=3)
  7 => string 'explode' (length=7)
  8 => string 'function' (length=8)

This is because our delimeter was space i.e. ‘ ‘ and we are spliting string ‘This is just another test of php explode function’.

Now let us take the same example of PHP explode function and pass limit as a positive number and the string will be exploded on the basis of space i.e. ‘ ‘.

<?php
$string = 'This is just another test of php explode function';
$delimiter = ' ';
$limit = 4;
$result_array = explode($delimiter, $string,$limit);
var_dump($result_array);
?>

Output of the above code will be:

array (size=4)
  0 => string 'This' (length=4)
  1 => string 'is' (length=2)
  2 => string 'just' (length=4)
  3 => string 'another test of php explode function' (length=36)

You can see that the because we have passed the limit to 4 only string is exploded 4 times for the $delimiter. In the 4th index rest of the string is available.

Now let us see what happens if we pass a delimiter which does not exist in the string:
<?php
$string = 'This is just another test of php explode function';
$delimiter = 'Ankur';
$limit = 4;
$result_array = explode($delimiter, $string,$limit);
var_dump($result_array);
?>

It will simply return an array and the first element of the array will be the whole string. This is because the delimiter ‘Ankur’ is not avaiable anywhere in $string. Output of the above code will be:

array (size=1)
  0 => string 'This is just another test of php explode function' (length=49)

Now let us pass delimiter as ” means a string which does not contain anything:
<?php
$string = 'Some test string';
$delimiter = '';
$result_array = explode($delimiter, $string);
var_dump($result_array);
?>

If you will run above program on your browser then result_array variable will contain boolean value false and you will get a warning. This is because your delimeter is blank. I got below output in my browser:
PHP Explode Example

Realtime PHP Explode Function Example

In the previous section we have seen some basic example of php explode function to understand its nature. Now below are some examples form real programming word where you can use explode function.

Getting Filename from directory path: If you know that the last part of your path string is file name then you can simply explode the path with string ‘/’ and fetch the filename like below code:
<?php
$path = '/Application/XAMPP/htdocs/a.php';
$temp_array =explode('/', $path);
print $temp_array[count($temp_array)-1]//print a.php
?>

Exploding Comma Seprated Names: If you have any string which contains comma seprated name(name could be person name, city name or name of animal etc) and you want to take it into an array then you can simply explode the string with delimeter comma using PHP explode function like below:
<?php
$names = 'Ankur,Ajay,Robert';
$arr_name = explode(',', $names);
print_r($arr_name);// Print Array ( [0] => Ankur [1] => Ajay [2] => Robert )
?>

Explode New Line from large string: Suppose, you have a large string which contains biography of user and you want to take every line in an array element then you can pass “\n” into delimeter to explode on the basis of new line like below:
<?php
$string ="I am ankur
I am a programmer";
var_dump(explode("\n", $string));
?>

PHP Explode() Function with List()

You can create sets of variables from any string using explode function. For example, you have a comma separated string contains an address in which first part is street, second is city and third is zip code and you want to take address, city, and zip into the different variables. So with the help of PHP explode function and list you can eaisly do this in a single line like below:
<?php
$address = '123 Park Stree, New Delhi,110044';
list($address, $city,$zip) = explode(',', $address);
?>

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

shares