PHP Explode Function | Convert String To Array

PHP explode Function is an inbuilt string function of PHP. Explode literally means to break down. The Function splits or breaks a string into an array of strings. It returns an array containing exploded sub-strings of the string. Sub-strings form by splitting the string by given string delimiter. In this article, we discuss the PHP Explode Function. Also, we will discuss a few examples demonstrating the usage of this function.

Also, read about PHP implode Function on Concatly!

Syntax

explode($delimiter, $string, $limit = 0);

ParametersI

PHP Explode Function expects two mandatory parameters and one optional parameter. The description of the parameters is as follows:

  • $delimiter: Delimiter or the separator is the mandatory parameter. It specifies where to break the string from. The function will split the string around the delimiter.
  • $string: Original string on which explode operation is to be performed. It is also a mandatory parameter.
  • $limit: It is an optional parameter and specifies the number of elements to be returned from the string. It can have 3 possible values:
    • Zero: It will return the whole string as one array element. This is the default value of this parameter.
    • Positive: It will return the number of specified sub-strings, with the last element containing the rest of the string.
    • Negative: It will return the sub-strings except for the last limit elements of the string.

Return Value

PHP Explode Function returns an array containing the sub-strings of the string exploded. Moreover, the return values also depend upon the limit passed.

PHP explode Function Diagram Concatly

Examples

Now let us discuss few examples demonstrating the use of explode function.

Example 1: Basic Usage

Let us first see a very basic example of splitting the string by ‘-‘ delimiter. You can split the string by any other delimiter that you want.

<?php 
$string = "Hi-how-are-you-doing?";
$explodedString = explode('-', $string);
print_r($explodedString);
?>
Output
Array
(
    [0] => Hi
    [1] => how
    [2] => are
    [3] => you
    [4] => doing?
)

As we can see, the string gets exploded wherever ‘-‘ delimiter is encountered and explode function returns an array containing those sub-strings.

Example 2: Passing Empty String

For instance, let us see what will happen if we pass an empty string to explode function.

$string = "";
$explodedString = explode('-', $string);
print_r($explodedString);
?>
Output
Array
(
    [0] = ; 
)

If you try to explode an empty string then explode function will return one empty array element!

In such cases, firstly, you can filter empty string beforehand by using array_filter or array_map function and then explode your string. Additionally, you can check if the string is empty using the PHP empty Function

Example 3: Positive Limit With Explode

Here we split the string with ‘-‘ delimiter and give positive value of the limit.

<?php 
$string = "Hi-how-are-you-doing?";
$explodedString = explode('-', $string, 3);
print_r($explodedString);
?>
Output

Array
(
    [0] => Hi
    [1] => how
    [2] => are-you-doing?
)

As we can see above, clearly the PHP Explode Function returns 3 elements. Here, first two are substrings of the string and the last element is the remaining string. Therefore, using a positive limit will return the number of elements equal to the limit with last element containing rest of the string and other elements containing exploded sub-strings of the string.

Example 4: Negative Limit With Explode

For example, let us now pass a negative limit to explode function and explode the string by ‘-‘ delimiter.

<?php 
$string = "Hi-how-are-you-doing?";
$explodedString = explode('-', $string, -2);
print_r($explodedString);
?>
Output

Array
(
    [0] => Hi
    [1] => how
    [2] => are
)

We can observe clearly that the PHP explode Function returns the element except the last number of elements which is passed as a negative limit.

Example 5: Using Zero Limit With Explode

Here, we pass 0 as the limit to explode function and explode the string by ‘-‘ delimiter. Additionally, this is the default value passed to the function.

<?php 
$string = "Hi,-how-are-you-doing?";
$explodedString = explode('-', $string, 0);
print_r($explodedString);
?>
Output

Array
(
    [0] => Hi,-how-are-you-doing?
)

As we can see from the above output that the PHP Explode Function returns the whole string as a single array element when we pass 0 as limit.

Example 6: Exploding String Containing Repeated Delimiters

What will happen if the string contains repeated delimiter? Let us see an example where we explode the string containing repeated ‘-‘ delimiter.

<?php 
$string = "Hi-how--are---you----doing?";
$explodedString = explode('-', $string);
print_r($explodedString);
?>
Output

Array
(
    [0] => Hi
    [1] => how
    [2] => 
    [3] => are
    [4] => 
    [5] => 
    [6] => you
    [7] => 
    [8] => 
    [9] => 
    [10] => doing?
)

Certainly, its evident from the above output that the PHP Explode Function returns empty array elements in place of the delimiters where they are repeated twice or more.

Conclusion

In this article, we discussed the PHP Explode Function and also used it in examples. You can learn more about it from the official documentation of PHP.

You may also go through PHP implode Function or learn more about PHP Array Functions on Concatly!

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *