Learn how to replace the deprecated PHP split function with modern alternatives like explode() and preg_split(). This guide covers performance, use cases, and best practices for secure and efficient string operations in PHP.
You dust off an old script, run it on a modern server, and it dies on a single line: a call to split(). The function is gone. It has been gone for years, and it is not coming back.
Here is the fix and the reasoning behind it. split() was deprecated in PHP 5.3.0 and removed for good in PHP 7.0.0. If you are moving legacy code forward, you replace it with one of two functions: explode() when you are splitting on a fixed string, and preg_split() when you are splitting on a pattern. Which one you reach for depends on what your old split() call was actually doing. Let’s walk through it.
Why Was split() Deprecated?
Two dates matter. split() was marked deprecated in PHP 5.3.0, so from that release on it threw a deprecation notice. It was then removed entirely in PHP 7.0.0, along with the rest of the POSIX regex (ereg) extension it belonged to. On PHP 7 or later, calling it is a fatal error, not a warning.
The Problem with split()
split() ran on POSIX Extended Regular Expressions (ERE). PHP already had a second, more capable regex engine in PCRE (Perl Compatible Regular Expressions), and keeping two engines alive was not worth it. So the whole POSIX family got dropped and PCRE became the standard. That is the real reason split() went away: not that it was slow, but that its entire extension was retired.
One detail trips people up. Because split() took a regular expression, its first argument was a pattern, not a literal delimiter. Plenty of code used it for plain splits where a comma was just a comma. Other code leaned on the regex to match a set of characters. That difference decides your replacement.
Why Use explode() or preg_split() Instead?
This is the whole decision. If your old split() pattern was really just a fixed string, use explode(). It never touches the regex engine, so it is faster and there is no pattern to get wrong. If the pattern genuinely needed regex, use preg_split(). It is the direct one-to-one replacement for split(): same idea, better engine. You rewrite the pattern in PCRE syntax, which mostly means wrapping it in delimiters like /.../.
Using explode() as a Replacement for split()
Most old split() calls were splitting on something simple: a space, a comma, a hyphen. For those, explode() is the right tool. It splits on a literal delimiter and skips regex entirely, which is why it is quick.
Basic Syntax of explode()
<?php
explode( string $delimiter, string $string, int $limit = PHP_INT_MAX ) : arrayThe explode() function takes three arguments:
- $delimiter: The character or string to split on.
- $string: The string to be split.
- $limit: (Optional) The maximum number of pieces. Left off, there is no limit.
Example: Splitting a String by Spaces
Example:
<?php
$string = "PHP is a powerful scripting language";
$words = explode( " ", $string );
print_r( $words );
?>That splits the string on each space into an array. The output is:
Array
(
[0] => PHP
[1] => is
[2] => a
[3] => powerful
[4] => scripting
[5] => language
)
When to Use explode()
Reach for explode() whenever your separator is a fixed character or string and no pattern is involved (spaces, commas, hyphens). It is the simplest, fastest choice, and it covers the majority of real cases.
Using preg_split() for Complex String Operations
When the split depends on a pattern (one or more spaces, a mix of commas and semicolons, anything variable) preg_split() is the answer. It is slower than explode() because it runs the regex engine, but that engine is exactly what you are paying for.
Basic Syntax of preg_split()
<?php
preg_split( string $pattern, string $subject, int $limit = -1, int $flags = 0 ) : arrayThe preg_split() function splits a string on a regular expression. It takes four arguments:
- $pattern: The regular expression to match.
- $subject: The string to split.
- $limit: (Optional) The maximum number of pieces.
- $flags: (Optional) Flags that adjust the behavior.
Example: Splitting a String by Multiple Spaces
Example:
<?php
$string = "PHP is a powerful scripting language";
$words = preg_split( '/\s+/', $string );
print_r( $words );
?>The pattern /\s+/ matches one or more whitespace characters, so runs of extra spaces collapse into clean splits:
Array
(
[0] => PHP
[1] => is
[2] => a
[3] => powerful
[4] => scripting
[5] => language
)
When to Use preg_split()
Use preg_split() when a single fixed delimiter will not cut it: multiple separators, whitespace of unknown length, or any real pattern. It costs more than explode(), but it is the only one of the two that can match a pattern at all.
Example: Splitting a String by Commas, Semicolons, and Spaces
Example:
<?php
$string = "apple, orange; banana grape";
$fruits = preg_split( '/[,;\s]+/', $string );
print_r( $fruits );
?>Here /[,;\s]+/ treats any run of commas, semicolons, or spaces as one separator, so the messy input comes out clean:
Array
(
[0] => apple
[1] => orange
[2] => banana
[3] => grape
)
Performance Considerations: explode() vs preg_split()
The trade-off is simple. explode() is faster because it never starts the regex engine, so it wins for single, fixed-delimiter splits. preg_split() carries the cost of that engine, and in return it handles patterns and multiple delimiters. Do not reach for regex when a plain string will do, and do not force explode() to handle a pattern it cannot.
- Use
explode(): When splitting by a simple, single-character delimiter. - Use
preg_split(): When working with complex patterns or multiple delimiters.
The Bottom Line
If you are porting old PHP forward, split() is one of the easy fixes. Look at what the call was doing. A fixed separator becomes explode(). A real pattern becomes preg_split(), with the pattern rewritten in PCRE form. That is the entire migration.
One honest caveat: do not assume every split() was a plain explode() in disguise. If the old first argument used character classes, alternation, or quantifiers, it was regex, and only preg_split() will reproduce it. Check the pattern before you swap, and you will not lose behavior in the move.


