PHP explode() Function (original) (raw)

Last Updated : 24 Jun, 2025

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,stringdelimiter, string delimiter,stringstring, int $limit = PHPINTMAX): array

**Parameters

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

**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 `

result=explode(",",result = explode(",", 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 `

result=explode("",result = explode(" ", 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 `

result=explode(",",result = explode(",", 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 `

result=explode(",",result = explode(",", result=explode(",",str); // Print the resulting array print_r($result); ?>

`

**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 `

result=explode(",",result = explode(",", 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

Best Practices

**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.