PHP explode: How to Split String into Array Elements in PHP

PHP Explode Function Example | PHP String Explode Tutorial

The explode() function in PHP allows us to break the string into smaller text, with each break occurring at the same symbol.

PHP explode

PHP explode() is a built-in function that splits the string by the specified string into array elements. The explode() function takes three arguments and returns an array containing string elements. The explode() function breaks the string into an array. The explode() function is binary-safe.

PHP array to string

To convert an array to a string in PHP, use the implode() function. The implode() function joins array elements with a string.

PHP string to array

To convert string to array in PHP, use the explode() function. The explode() function breaks the string into an array, but the implode function returns a string from the elements of an array.


See the syntax.



The separator parameter is required to specify where we break the string.

The string parameter is required. It is the string to split.

The limit parameter is optional and specifies the number of array elements to return.

Implementation of explode() function

See the following example.


// app.php

$brandsA = 'LouisVuitton Hermès Gucci Prada Chanel Cartier';
$arrA = explode(" ", $brandsA);

See the below output.

PHP Explode Function Example

Passing limit parameter

See the following code where we pass the third parameter limit and see the output.


// app.php

$strA = 'ronaldo|messi|neymar|bale';
$arrA = explode("|", $strA, 2);

See the output.

The limit parameter example

Use the negative limit and see the result.


// app.php

$strA = 'ronaldo|messi|neymar|bale';
$arrA = explode("|", $strA, -1);

See the below output.

Use the negative limit

Passing Multiple delimiters

You can pass the multiple delimiters to the explode() function.

See the following code.


// app.php

function multipleexplode ($delimiters,$string) {
  $phase = str_replace($delimiters, $delimiters[0], $string);
  $processed = explode($delimiters[0], $phase);
  return  $processed;

$longStr = "appdividend is cool: krunal is author, and this will be exploded. this also | this one too :)";
$op = multipleexplode(array(",",".","|",":"),$longStr);

See the output.

Multiple delimiters in PHP Explode Function

How to split empty string in PHP

To split the empty string in PHP, use the explode() function.


// app.php

$strA = "";
$result = explode(",", $strA);

See the following example.

Split an empty String

If you split the empty string, you get the one-element array with 0 as the key and the empty string for the value.

If we want to solve this, use the array_filter() without a callback. Quoting the manual page, “If the callback function is not supplied, the array_filter() function will remove all the entries of input that are equal to FALSE.”

Trim whitespaces using explode()

We can use the explode() function to trim the white space from the string.

With the help of the array_map() and explode() functions, we can trim the white spaces and split the string into an array. See the following code.


// app.php

$str = "one  ,two  ,       three  ,  four    "; 

See the output.

Trim whitespaces using explode() method

Explode does not parse the string by delimiters, in the sense that we expect to find tokens between the starting and ending delimiter, but instead splits the string into pieces by using the string as the boundary of each part.

Once that boundary is discovered, the string is split. Any data preceding that limit is irrelevant since the parts are determined when a limit is found.

It should be said that when an empty delimiter is passed to explode, the function returns false and emits a warning. See the following code.


// app.php

$data = 'aqswwdwdwdwdwdw';
print_r(explode('', $data));

See the output.

empty delimiter in explode() function

PHP explode vs. split

The main difference between the split() and explode() functions is splitting a large string. Both the functions are used to split a string. However, the split() function is used to split a string using a regular expression, while the explode() function is used to split a string using another string.

One more point, the split() function has been DEPRECATED as of PHP 5.3.0. Therefore, relying on this feature is discouraged; instead, use the explode() function.

The preg_split() is faster and uses PCRE regular expressions for regex splits.


To break the string and create the array, use the explode() function. That is it for splitting a string into an array the PHP.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.