PHP explode() Function to Split String into Array

The explode() function in PHP is a built-in function that is used to split a string into an array based on a delimiter or pattern used. In other words, this function is used to convert a string into a PHP array.

For example, if you have a string separated by comma, the explode() function can split it into an array of individual values.

<?php
$string = "Apple,Banana,Orange,Mango ";
$result = explode(",", $string);
print_r($result);
?>
Output:
      Array
      (
        [0] => Apple
        [1] => Banana
        [2] => Orange
        [3] => Mango 
      )

Syntax of PHP explode() Function


The general syntax to define explode() function in PHP is as follows:

explode(string $separator, string $string, int $limit): array

The explode() function takes three parameters, such as:

  • $separator: This parameter represents the character that is to be used as break point between array elements.
  • $string: This is an input string that has to be converted into an array using the specified separator.
  • $limit: This is the third parameter which specifies the maximum number of elements in the returned array.
    • If the limit is set to positive, the return array will contain the maximum of limit elements with the last element containing the rest of the string.
    • If the limit is set to negative, all components except the last limit are returned.
    • If limit parameter is zero, it is treated as 1.

Return

The explode() function returns an array of strings created by splitting the original string at each point where the separator occurs. If the separator is an empty string (” “), the explode() function throws a ValueError starting from PHP 8.0.


If the separator contains a value that is not contained in string and a negative limit is used, the function returns an empty array. Otherwise, it returns an array containing string. However, if the separator is not found in string and the limit is positive or not provided, it returns an array with the entire string as a single element:

If the separator values appear at the start or end of string, said values will be added as an empty array value either in the first or last position of the returned array respectively.

Examples of explode() Function in PHP


Example 1: Let’s write a PHP program in which we will split a string into an array using a comma as the separator.

<?php
$colors = "Red,Green,Blue,Orange ";
$result = explode(",", $colors);
print_r($result);
?>
Output:
       Array
       (
         [0] => Red
         [1] => Green
         [2] => Blue
         [3] => Orange 
       )


Example 2: Let’s write a PHP program in which we will split a string into an array using a space as the separator.

<?php
$string = "PHP is awesome";
$result = explode(" ", $string);
print_r($result);
?>
Output:
      Array
     (
       [0] => PHP
       [1] => is
       [2] => awesome
     )

Example 3: Let’s write a PHP program in which we will set the limit parameter in the explode() function.

<?php
$str = "One,Two,Three,Four,Five";
$result = explode(",", $str, 3);
print_r($result);
?>
Output:
      Array
      (
        [0] => One
        [1] => Two
        [2] => Three,Four,Five
      )

In this example, we have set the limit to 3. Therefore, the explode() function returns 3 elements, with the last element containing the rest of the string.

Advanced Examples on PHP explode() Function


Example 4: Let’s write a PHP program in which we will set negative limit in the explode() function.

<?php
$str = "a,b,c,d,e,f";
$result = explode(",", $str, -1);
print_r($result);
?>
Output:
      Array
     (
       [0] => a
       [1] => b
       [2] => c
       [3] => d
       [4] => e
     )

In this example, we have set a negative limit which omits the last N elements. In this case, -1 omits the last element.

Example 5:

<?php
$intro = "John,Doe,28,USA";
$data = explode(",", $intro);
echo "Name: $data[0] $data[1], Age: $data[2], Country: $data[3]";
?>
Output:
      Name: John Doe, Age: 28, Country: USA

Example 6: Breaking Query Strings

<?php
$query = "Name=John&Age=30&Country=USA";
$pairs = explode("&", $query);

foreach ($pairs as $pair) {
   list($key, $value) = explode("=", $pair);
   echo "$key: $value\n";
}
?>
Output:
       Name: John
       Age: 30
       Country: USA

In this example, we have defined a string named $query, representing a query string commonly found in URLs. It contains key-value pairs separated by &, and each key-value pair is separated by =. The explode() function splits the string into an array of key-value pairs using the & character as the delimiter or separator.

Then, we have used a foreach loop to iterate over each key-value pair string. Inside the loop, each $pair (like “Name=John”) is split into two parts using explode(“=”, $pair). The list($key, $value) assigns the first part to $key and the second to $value. The echo statement prints the key and its corresponding value, separated by a colon (:) and followed by a newline (\n).


In this tutorial, you learned about the explode() function, which is used to split a string into an array based on a delimiter in PHP. I hope you now understand how to use the explode() function and have practiced all the examples provided.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the founder of Scientech Easy and a passionate coding educator with 8 years of professional experience in Java, Python, web development, and core computer science subjects. With expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He consistently publishes in-depth tutorials, practical coding examples, and valuable learning resources for beginners as well as working professionals. Each article is carefully researched and reviewed to ensure accuracy, quality, and real-world relevance.