Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP code still calls eregi(), which was deprecated and then removed

ErrorDeprecated: Function eregi() is deprecated in

Short answer

eregi() belongs to the ereg extension, which PHP deprecated in 5.3 and removed in 7.0. On a current PHP the call cannot work, so port it. Replace each call with preg_match(), put the pattern between delimiters and add the i modifier for case-insensitive matching, and check the result with ===.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every call to the ereg family in the project, not only the one in the error. The whole extension is gone, so the next call will fail the same way.

    grep -rnwE "ereg|eregi|ereg_replace" --include=*.php .
  2. 2Match what you see, then jump to its fix

    1. 1The warning or error names eregi() and a line in a template, plugin or old includeA case-insensitive check still uses eregi()Fix
    2. 2After fixing eregi(), the next error names ereg() or ereg_replace()Other functions of the same extension are left in the codeFix

What does the error look like?

  • Deprecated: Function eregi() is deprecated in
  • eregi() is deprecated

Cause 1 of 2

A case-insensitive check still uses eregi()

1Check

The PHP 7.0 RFC for removing deprecated functionality lists ext/ereg as deprecated since PHP 5.3 and as removed, implemented in PHP 7.0. Read the call and note the pattern and the string it is matched against.

grep -rn "eregi(" --include=*.php .

2Fix

Use preg_match() from the PCRE extension, as the RFC says. The manual's example shows the i after the closing delimiter making the search case-insensitive.

<?php
// before (removed)
// if (eregi("php", $subject)) { ... }

// after
if (preg_match('/php/i', $subject) === 1) {
    // matched
}

3Verify

Check the file for syntax errors, then run the page that used to warn and confirm it matches the same strings as before.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

Other functions of the same extension are left in the code

1Check

The RFC removes ext/ereg as a whole extension, not one function. Every function it provided fails the same way, so search for all of them before you deploy.

grep -rnwE "ereg|ereg_replace" --include=*.php .

2Fix

Port each of them to the PCRE extension, which the RFC names as the replacement. If you cannot port a third-party plugin, take a release of it that supports your PHP version.

<?php
// ereg() match test, ported
if (preg_match('/^[a-z]+$/', $name) === 1) {
    // name is letters only
}

3Verify

Repeat the search from the first step. It should return no hits in your own code.

grep -rnwE "ereg|eregi|ereg_replace" --include=*.php .

Confirmed by wiki.php.net

Did this fix it?

How do I stop it coming back?

  • Run the code on the PHP version you will deploy, with error reporting on, before the upgrade.
  • Test the return value of preg_match() with ===, because the manual says it returns 1, 0, or false on failure.
  • Keep themes and plugins on releases that support your PHP version, instead of patching their code by hand.
  • Add a search for the old function names to your CI, so they cannot come back.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.