Introduction
While working with strings in PHP, developers often need to break a string into multiple parts. For example, you may want to split a sentence into words or separate values stored in a comma-separated string.
PHP provides several functions to perform this task, and two commonly discussed functions are:
- explode()
- split()
Both functions are used to split strings into arrays, but they work differently and have different use cases.
However, it is important to note that split() has been deprecated and removed from modern versions of PHP, and developers are encouraged to use explode() or preg_split() instead.
What is explode() in PHP?
The explode() function is used to split a string into an array based on a specified delimiter.
A delimiter is a character that separates values in a string, such as:
- Comma (,)
- Space ( )
- Dash (-)
- Colon (:)
explode() breaks the string wherever the delimiter appears and returns an array.
Syntax of explode()
explode(separator, string, limit);
Parameters
- separator – The character used to split the string.
- string – The string to be split.
- limit (optional) – Specifies the number of array elements.
Example of explode()
$text = "PHP,Java,Python,JavaScript";
$languages = explode(",", $text);
print_r($languages);
Output
[1] => Java
[2] => Python
[3] => JavaScript
)
Explanation
In this example:
- The string contains programming languages separated by commas.
- The explode() function splits the string using “,” as the delimiter.
- The result is stored in an array.
Example Using explode() with Space
$sentence = "PHP is a server side language";
$words = explode(" ", $sentence);
print_r($words);
Output
[1] => is
[2] => a
[3] => server
[4] => side
[5] => language
)
Here, explode() splits the sentence into individual words.
Using explode() with Limit
The limit parameter controls how many elements will be created.
$text = "red,green,blue,yellow";
$result = explode(",", $text, 2);
print_r($result);
Output
Explanation:
- Only two elements are created.
- The remaining string is stored in the second element.
What is split() in PHP?
The split() function was previously used in PHP to split a string using regular expressions.
Unlike explode(), which splits based on a simple delimiter, split() allowed more complex pattern matching.
Syntax of split()
split(pattern, string);
Parameters
- pattern – Regular expression pattern
- string – The string to split
Example of split()
$text = "PHP-Java-Python";
$result = split("-", $text);
print_r($result);
Output
[1] => Java
[2] => Python
)
Important Note: split() is Deprecated
The split() function was deprecated in PHP 5.3 and removed in PHP 7.
This means modern PHP applications should not use split().
Instead, developers should use:
- explode() (for simple splitting)
- preg_split() (for regular expression splitting)
Example using preg_split():
$text = "PHP-Java-Python";
$result = preg_split("/-/", $text);
print_r($result);
Difference Between explode() and split()
Even though both functions split strings, they work differently.
| Feature | explode() | split() |
|---|---|---|
| Purpose | Splits string using delimiter | Splits string using regex |
| Performance | Faster | Slower |
| Regex Support | No | Yes |
| PHP Support | Fully supported | Deprecated / removed |
| Recommended | Yes | No |
Real Life Example
Suppose you store multiple email addresses in a single string.
john@gmail.com, rahul@gmail.com, neha@gmail.com
You can separate them using explode().
$emails = "john@gmail.com,rahul@gmail.com,neha@gmail.com";
$emailList = explode(",", $emails);
print_r($emailList);
Output:
[1] => rahul@gmail.com
[2] => neha@gmail.com
)
This allows you to process each email individually.
When Should You Use explode()?
Use explode() when:
- You have a simple delimiter
- You want better performance
- You are splitting CSV-style data
Example:
explode(",", $data);
When Should You Use preg_split()?
Use preg_split() when:
- You need regular expressions
- The delimiter may have multiple patterns
Example:
preg_split("/[\s,]+/", $text);
This splits a string using spaces or commas.
Conclusion
Both explode() and split() were designed to split strings into arrays in PHP.
The split() function is deprecated and removed from modern PHP versions due to performance and compatibility reasons.
Today, developers use explode() for simple string splitting and preg_split() for advanced pattern-based splitting.
Understanding the difference between explode() vs split() helps developers write efficient, modern, and maintainable PHP code.
Split vs Explode – Objective Questions (MCQs)
Q1. Which function splits a string using regular expression?
Q2. Which function splits a string using a delimiter?
Q3. split() is ______ in PHP 7 and above.
Q4. Both split() and explode() return ______.
Q5. Which is faster for simple delimiter splitting?