In PHP 7.0 and later, mb_eregi_replace() was officially deprecated and was completely removed after PHP 7.1. This has bothered many developers who rely on regular replacement of multi-byte strings. Especially when a case-insensitive replacement of Unicode strings (such as Chinese, Japanese, etc.) is required, it is particularly important to find an equivalent and more powerful alternative.
Fortunately, PHP's preg_replace() function, combined with the correct modifier and Unicode support, can implement even more than mb_eregi_replace() . This article will gradually introduce how to perfectly replace mb_eregi_replace( ) with preg_replace () and implement powerful Unicode regular matching.
mb_eregi_replace() is a case-insensitive regular replacement function in a multibyte string extension (mbstring). However, its problem is:
The syntax is older and does not support Perl style regularity.
Poor performance.
With modern Unicode, limited processing capacity and high maintenance costs.
Therefore, it is recommended to use the more modern and feature-rich preg_replace() .
Suppose we used:
mb_eregi_replace("test", "replace", $text);
We can rewrite it like this:
preg_replace("/test/ui", "replace", $text);
Here, the u modifier means turning on Unicode mode, and i means ignoring upper and lower case.
Suppose we want to replace all "apples" in a paragraph of text with "bananas" and are case-insensitive:
$text = "My favorite apples,And apple juice。";
$result = preg_replace("/apple/ui", "banana", $text);
echo $result;
Output:
我最喜欢吃banana,还有banana汁。
Consider replacing the Unicode parameter in a URL (such as Chinese tag):
$url = "https://m66.net/search?q=apple手机";
$replaced = preg_replace("/apple/ui", "banana", $url);
echo $replaced;
Output:
https://m66.net/search?q=banana手机
As you can see, preg_replace() can also handle URLs containing Unicode.
If you want to match more variants such as "Apple Phone", "Apple Computer", "Apple Pie", etc., you can use more complex regular expressions:
$text = "我有一台applecomputer,也吃了applegroup。";
$pattern = "/apple(computer|group)?/ui";
$replacement = "banana";
$result = preg_replace($pattern, $replacement, $text);
echo $result;
Output:
我有一台banana,也吃了banana。
Always remember to add the u modifier to enable Unicode mode.
preg_replace() supports more complex regular syntax (such as assertions, named groups, etc.), and is more flexible than mb_eregi_replace() .
If the replacement content contains a backslash ( \ ) or a dollar sign ( $ ), use the escape process in preg_quote() or double quotes.
While deprecated mb_eregi_replace() may be uncomfortable, preg_replace() not only serves as a perfect alternative, but also provides more powerful Unicode regular support. As long as you master the use of u and i modifiers, you can easily deal with any multibyte character replacement requirement.
If you need to deal with more complex Unicode pattern matching, you might as well learn more about the syntax and features of PCRE regular expressions to unlock more possibilities in the PHP regular world.