PHP explode: Splitting strings into arrays

In this tutorial, we look at the PHP explode function. We take a close look at its parameters, return values and it’s limitations.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - PHP foreach:

PHP explode - What is explode in PHP?

PHP explode() is a built-in function that is used to split a string into a string array. It splits a string based on a specified separator that we pass as an argument. Apart from this functionality, the explode method also has a third parameter, “limit” that can be used to manipulate the number of elements in the array. We take a closer look at this below.

Syntax of PHP explode:

explode(separator,string,limit)

Parameters:

  • Separator - Required, used to specify where the string should be split
  • String - Required, the string that you want to split
  • Limit - Optional, used to specify the number of elements in the string array.
    • Positive Value - If a positive value is passed as a parameter, it denotes the number of elements the string array should contain. In case the value is lesser than the number of elements in the array, the N-1th element of the string array would contain all the remaining elements.
    • Negative Value - If a negative value is passed the last element will be trimmed off and the and the remaining part of elements would be returned as an array.
    • Zero - If zero is passed the element will contain only elements which would be the entire string.

Return Value:

If no limit argument is passed the string array would contain all the elements split by the separator. If a limit value is passed, the return values would be based on that.

Code & Explanation:

<?php

    // string
    $string_1 = "Hire Freelance Developer";

    // using explode without a limit
    print_r(explode(" ",$string_1));

?>

The output of the above code snippet would be:

Array
(
    [0] => Hire,
    [1] => Freelance
    [2] => Developers

)

In the above code block, since a limit was not passed the returned array contains a string with all the elements.

Now let’s look at an example using the limit:

<?php

    // string
    $string_1 = "Hire the top Freelance Developer";

    // a positive limit
    print_r(explode(" ",$string_1,3));
    // a negative limit
    print_r(explode(" ",$string_1,-1));

?>

The output of the above code snippet would be:

Array
(
    [0] => Hire,
    [1] => the 
    [2] => top Freelance Developer
)
Array
(
    [0] => Hire,
    [1] => the
    [2] => top
    [3] => freelance
)

In the first array, since the limit value was less than the number of elements all the remaining elements were passed in the last element. In the last array, since a negative value was passed the last element was removed and all the other values were passed as an array.

Limitation and Caveats - PHP Explode

Note that Explode will throw a Valueerror if the separator parameter is an empty string. Other than this, there are no major constraints in the explode function.