PHP explode() Function

Usage — The PHP explode() function is used to split a string by another string.

It has the following syntax:

PHP

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

Here is an example of using explode():

PHP

$str = "We will play basketball tomorrow.";
/*  Output —
(
    [0] => We
    [1] => will
    [2] => play
    [3] => basketball
    [4] => tomorrow.
) */
print_r(explode(" ",$str));

Return Value — This function returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

PHP Version and Changelog — The explode() function is available in PHP 4, PHP 5, PHP 7. The support for negative limits was added in PHP version 5.1.0.

Relevant Functions — Other related PHP functions that you should know about are: preg_split which splits a string by a regular expression, str_split() which converts a string to an array, mb_split which splits a multibyte string using regular expression, str_word_count() which returns information about words used in a string, strtok() which tokenizes a string and implode() which joins array elements with a string.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

delimiter

The delimiter parameter is used to specify the boundary string. In other words, the delimiter parameter specifies the string which will be used to split the original string.

string

The string parameter is used to specify the input string that we want to split in smaller parts.

limit

The limit parameter is used to specify how many smaller components should be returned by explode.

If limit is set to something positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

Working Examples

Here are some examples of using the explode() function:

PHP

$zoo = "Lion Tiger Cow Pig Deer Elephant";
$animals = explode(" ", $zoo);

var_dump(explode(" ", $zoo));
/* Output —
array(6) {
    [0]=>
    string(4) "Lion"
    [1]=>
    string(5) "Tiger"
    [2]=>
    string(3) "Cow"
    [3]=>
    string(3) "Pig"
    [4]=>
    string(4) "Deer"
    [5]=>
    string(8) "Elephant"
} */

// Output — Lion
echo $animals[0];

// Output — Tiger
echo $animals[1];

explode() return examples

PHP

// A string that doesn't contain the delimiter will simply return a one-length array of the original string.
$greeting1 = "Hello";
$greeting2 = "Hello, Hi";
$greeting3 = ',';

var_dump(explode(',', $greeting1));
/* Output —
array(1) {
    [0]=>
    string(5) "Hello"
} */

var_dump(explode(',', $greeting2));
/* Output —
array(2) {
    [0]=>
    string(5) "Hello"
    [1]=>
    string(3) " Hi"
} */

var_dump(explode(',', $greeting3));
/* Output —
array(2) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
} */

limit parameter examples

PHP

$zoo = "Lion Tiger Cow Pig Deer Elephant";

var_dump(explode(" ", $zoo, 4));
/* Output —
array(4) {
    [0]=>
    string(4) "Lion"
    [1]=>
    string(5) "Tiger"
    [2]=>
    string(3) "Cow"
    [3]=>
    string(17) "Pig Deer Elephant"
} */

var_dump(explode(" ", $zoo, -4));
/* Output —
array(2) {
    [0]=>
    string(4) "Lion"
    [1]=>
    string(5) "Tiger"
} */

var_dump(explode(" ", $zoo, -2));
/* Output —
array(4) {
    [0]=>
    string(4) "Lion"
    [1]=>
    string(5) "Tiger"
    [2]=>
    string(3) "Cow"
    [3]=>
    string(3) "Pig"
} */

var_dump(explode(" ", $zoo, 0));
/* Output —
array(1) {
    [0]=>
    string(32) "Lion Tiger Cow Pig Deer Elephant"
} */

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. Here is my approach to have exploded output with multiple delimiter.

    PHP

    //$delimiters has to be an array
    //$string has to be an array
    function multiexplode ($delimiters,$string) {
        $ready = str_replace($delimiters, $delimiters[0], $string);
        $launch = explode($delimiters[0], $ready);
        return  $launch;
    }
    $text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
    $exploded = multiexplode(array(",",".","|",":"),$text);
    
    print_r($exploded);
    /* Output —
    Array
    (
        [0] => here is a sample
        [1] =>  this text
        [2] =>  and this will be exploded
        [3] =>  this also
        [4] =>  this one too
        [5] => )
    )
    */
    

    Suggested by - php at metehanarslan dot com

  2. Beaware splitting empty strings.

    PHP

    $str = "";
    $res = explode(",", $str);
    
    var_dump($res);
    /* Output —
    array(1) {
      [0]=>
      string(0) ""
    }
    */
    

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

    PHP

    $str = "";
    $res = array_filter(explode(",", $str));
    
    var_dump($res);
    /* Output —
    array(0) {
    }
    */
    

    Suggested by - tiago dot dias at flow-me dot com

  3. The comments to use array_filter() without a callback to remove empty strings from explode’s results miss the fact that array_filter() will remove all elements that, to quote the manual, “are equal to FALSE”.

    This includes, in particular, the string “0”, which is NOT an empty string. If you really want to filter out empty strings, use the defining feature of the empty string that it is the only string that has a length of 0. So:

    PHP

    array_filter(explode(':', "1:2::3:0:4"), 'strlen');
    

    Suggested by - Hayley Watson

Further Reading

  1. You can read more about the PHP explode() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top