Open In App

PHP explode() Function

Last Updated : 24 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The explode() function in PHP is used to split a string into an array based on a specified delimiter. This function is commonly used when you need to break down a string into individual parts. The explode() function is simple to use and highly efficient for splitting strings.

Syntax:

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

Parameters

The explode function accepts three parameters, of which two are compulsory and one is optional. All three parameters are described below.

  • $delimiter (string): This is the character or string used to divide the input string.
  • $string (string): This is the string that you want to split.
  • $limit (optional, int): This is the maximum number of elements to return. If not specified, it will return all possible elements.

Return Type

The return type of the explode() function is an array of strings.

Common Uses of explode() Function

1. Splitting a String by a Comma

PHP
<?php
$str = "Riya,Rimjhim,Ayushi";  // Define a string with comma-separated names
// Use explode() to split the string by the comma delimiter
$result = explode(",", $str);
// Print the resulting array
print_r($result);  // Output: Array ( [0] => Riya [1] => Rimjhim [2] => Ayushi )
?>

Output
Array
(
    [0] => Riya
    [1] => Rimjhim
    [2] => Ayushi
)

Explanation: In this example, the string is split by commas and the result is an array of individual fruit names.

2. Splitting a String by Space

PHP
<?php
$str = "Riya Rimjhim Ayushi";  // Define a string with space-separated names
// Use explode() to split the string by the space delimiter
$result = explode(" ", $str);
// Print the resulting array
print_r($result);  // Output: Array ( [0] => Riya [1] => Rimjhim [2] => Ayushi )
?>

Output
Array
(
    [0] => Riya
    [1] => Rimjhim
    [2] => Ayushi
)

Explanation: In this code, the string is split into words by spaces, and the result is an array with each word as a separate element.

3. Using the $limit Parameter

The $limit The parameter allows you to specify how many elements to return. This is useful if you want to limit the number of splits.

PHP
<?php
$str = "Riya,Rimjhim,Ayushi";  // Define a string with comma-separated names
// Use explode() to split the string by the comma delimiter, limiting to 3 parts
$result = explode(",", $str, 3);
// Print the resulting array
print_r($result);  // Output: Array ( [0] => Riya [1] => Rimjhim [2] => Ayushi )
?>

Output
Array
(
    [0] => Riya
    [1] => Rimjhim
    [2] => Ayushi
)

Explanation: This code splits the string "Riya,Rimjhim,Ayushi" into an array using the comma as a delimiter, and limits the result to 3 parts, then prints the resulting array of names.

Special Cases to Consider with explode() in PHP

When we are using the explode() In PHP, you need to handle some special cases like empty strings, missing delimiters, or extra delimiters. Here are some common cases and how explode() works in each case.

1. Empty String

If the string is empty, then it will return an array with a single empty element.

PHP
<?php
$str = "";  // Define an empty string
// Use explode() to split the empty string by the comma delimiter
$result = explode(",", $str);
// Print the resulting array
print_r($result);
?>

Output
Array
(
    [0] => 
)

Explaination: This code splits an empty string using a comma as the delimiter, and the string is empty, then the result is an array with one empty element.

2. Delimiter Not Found

If the delimiter is not found in the string, then the explode() Returns an array with the entire string as a single element.

PHP
<?php
$str = "Riya";  // No comma in the string
$result = explode(",", $str);
print_r($result);  // Output: Array ( [0] => apple )
?>

Output
Array
(
    [0] => Riya
)

Explaination: This code splits the string "apple" using a comma as the delimiter, and if there is no comma, the entire string is returned as a single element in the array.

Important Notes

  • If the delimiter is not found, explode() will return an array containing the original string.
  • If the limit is set to -1, the function will split the string as much as possible.

Best Practices

  • Use explode() for simple delimiters because it is great when the delimiter is predictable, like commas or spaces.
  • Use $limit for controlling splits and this method is helpful when you want to restrict the number of splits in the string.
  • Avoid explode() function for complex data, it helps to parse JSON or XML, by using these functions like json_decode() or simplexml_load_string() it will give you better results.

Conclusion

The explode() function is a simple and powerful tool in PHP for breaking down a string into an array. Whether you are splitting a CSV string, extracting words or separating data, the explode() function is an efficient way to handle string manipulation in PHP.


Practice Tags :

Similar Reads