When using PHP for string processing, the mb_eregi_replace function used to be one of my common tools. This function can implement regular case-insensitive replacement of multi-byte strings, which is very suitable for processing text containing multi-byte characters such as Chinese. However, in the PHP 7.3+ environment, I suddenly found that using mb_eregi_replace in the code would cause warnings or even errors. I thought this function had been deprecated, but later I studied it in depth and found that the truth was not the case.
Simply put, mb_eregi_replace is a function provided by the mbstring extension to perform search replacement of regular expressions in multibyte strings, and is case-insensitive.
<?php
$original = "Hello World! hello world!";
$result = mb_eregi_replace("hello", "hi", $original);
echo $result; // Output:hi World! hi world!
?>
The function of this function is very useful in multi-byte string processing, especially when case matching is required.
In PHP 7.3 and above, mb_eregi_replace will prompt a warning similar to the following:
Deprecated: mb_eregi_replace(): This function is deprecated
This has led many developers (including me) to mistakenly believe that PHP official has deprecated this function. So we tried to use other alternatives, such as using mb_ereg_replace with manual lowercase, or using preg_replace instead.
In fact, starting from PHP 7.3, the underlying regular engine that mb_eregi_replace depends on has changed. PHP official migrated the regular engine from mbregex to PCRE (Perl Compatible Regular Expressions), and the mb_eregi_replace function itself is implemented based on mbregex , resulting in its internal mechanism being marked as deprecated, but the function still exists.
That is to say:
The function has not been completely removed, and it is only officially marked as "may be abandoned in the future", reminding developers to make a gradual transition;
This also explains why it is still available in some PHP versions, but gives a warning;
The maintenance status of the underlying mbregex regular library is deteriorating and is no longer recommended.
Since mb_eregi_replace has a risk of abandonment, it is recommended to use the following alternative:
<?php
$original = "Hello world! hello world!";
$pattern = "/hello/u"; // u Modifiers support multibytes,i Modifiers are case-insensitive
$result = preg_replace($pattern . "i", "hi", $original);
echo $result; // Output:hi world! hi world!
?>
The point here is:
The u modifier ensures that regular expressions are processed in UTF-8 multibytes;
The i modifier implementation ignores case;
preg_replace is a modern regular replacement function officially recommended by PHP.
PHP 8.0 has added mb_str_ireplace , which is a multi-byte case-insensitive string replacement function. The syntax is simple:
<?php
$original = "Hello world! hello world!";
$result = mb_str_ireplace("hello", "hi", $original);
echo $result; // Output:hi world! hi world!
?>
This is the easiest alternative if the project runtime environment supports PHP 8.0 and above.
mb_eregi_replace is indeed marked as deprecated in PHP 7.3+, but is not completely removed;
This abandonment originates from the underlying regular engine switching rather than directly removing functions;
It is recommended to use preg_replace with u and i modifiers instead;
If you can upgrade to PHP 8.0 or above, you can use mb_str_ireplace to replace it;
In the future, PHP versions may completely remove mb_eregi_replace , and the earlier the code is, the better.
I hope this article can help you clarify the current situation of mb_eregi_replace , avoid blindly worrying about and using outdated functions, and maintain the modernity and stability of the code.