When dealing with multibyte strings (such as UTF-8 encoded Chinese, Japanese, Korean, etc.), standard regular expression functions may not handle character set issues correctly. To solve this problem, PHP provides a set of multibyte string functions, one of which is mb_eregi_replace . This article will explain how to use this function to replace all numbers in a string with specified characters, such as an asterisk ( * ).
mb_eregi_replace is one of PHP's multibyte string extension functions. It works similar to eregi_replace , but it can handle multibyte characters correctly. The basic syntax is as follows:
string mb_eregi_replace ( string $pattern , string $replacement , string $string [, string $option = "msri" ] )
Parameter description:
$pattern : The regular expression to match.
$replacement : A string used to replace matching content.
$string : The target string to be processed.
$option (optional): Match option, which includes multiple lines ( m ), dot matching new lines ( s ), ignoring case ( i ), etc. by default.
?? Note: The mb_eregi_replace function has been removed in PHP 7.0.0 and is not recommended for use in new projects. If you are still using PHP 5.x, you can use it; otherwise you should use preg_replace instead.
Suppose we have the following string:
$str = "userIDyes12345,验证码yes67890";
We want to replace all the numbers in it with * , we can use the following code:
<?php
mb_internal_encoding("UTF-8"); // Set internal encoding
$str = "userIDyes12345,验证码yes67890";
// use mb_eregi_replace Replace all numbers
$result = mb_eregi_replace("[0-9]", "*", $str);
echo $result;
?>
The output result is:
userIDyes*****,验证码yes*****
In this example, [0-9] means matching any one-bit number. If you want to match multiple digits and replace them with an asterisk, you can use the following pattern:
$result = mb_eregi_replace("[0-9]+", "*", $str);
Output:
userIDyes*,验证码yes*
Considering that mb_eregi_replace has been abandoned in newer versions, it is recommended to use preg_replace :
<?php
$str = "userIDyes12345,验证码yes67890";
$result = preg_replace("/\d+/", "*", $str);
echo $result;
?>
preg_replace is a PCRE-based regular expression function that also supports UTF-8. Just make sure to use the u modifier:
$result = preg_replace("/\d+/u", "*", $str);
This number replacement is often used for:
Data desensitization : Hide sensitive information such as ID number and mobile phone number.
Log filtering : prevents numbers from leaking into debug information.
Interface beautification : Convert numbers into visual placeholders such as * or ?.
For example, in a user information protection interface provided by m66.net, a similar method is used to process input strings to protect user privacy.
Although mb_eregi_replace provides powerful multibyte support in older versions of PHP, since it has been abandoned, developers are advised to use preg_replace in new projects and incorporate Unicode modifiers. No matter which method is used, the number in the string can be replaced flexibly to meet the dual requirements of security and aesthetics.