Article

Replacing split()

Remove split() safely: lifecycle, modern replacements, and migration examples for legacy PHP apps.

split() (from ext/ereg) divided a string using a POSIX regular expression. It is unrelated to JavaScript’s String.split mental model and is easy to confuse with explode().

Lifecycle (php.net)

  • Deprecated: ext/ereg deprecated earlier; avoid in new code as of PHP 5.3+ guidance era
  • Removed: PHP 7.0.0 (with ext/ereg)

Modern replacement

Use preg_split() for regex splits or explode()/str_getcsv() for literal delimiters.

Migration example

# LEGACY — split()
$parts = split('[:,]', $line);

# MODERN
$parts = preg_split('/[:,]/', $line);
# literal delimiter:
$parts = explode(',', $line);

Scan before cutover

Search the tree for split() and paste samples into the PHP Version Compatibility Checker and PHP Deprecated Checker.

Related tools

Related reading