Article

Replacing ereg()

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

ereg() matched a POSIX regular expression against a string. Case-insensitive eregi() and replacement cousins shared the same removed extension.

Lifecycle (php.net)

  • Deprecated: Deprecated as of PHP 5.3.0
  • Removed: PHP 7.0.0

Modern replacement

Use preg_match() / preg_replace() with Perl-compatible patterns. Remember delimiters and escaping differences.

Migration example

# LEGACY — ereg()
if (ereg('^[A-Za-z0-9]+$', $token)) {
    accept($token);
}

# MODERN
if (preg_match('/^[A-Za-z0-9]+$/', $token)) {
    accept($token);
}

Scan before cutover

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

Related tools

Related reading