Function name: mb_ereg_replace()
Function description: The mb_ereg_replace() function is used to replace it with regular expressions in a string. Unlike the normal ereg_replace() function, the mb_ereg_replace() function can handle multibyte characters.
Applicable version: PHP 4 >= 4.2.0, PHP 5, PHP 7
Usage: string mb_ereg_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] )
parameter:
- $pattern: A string that specifies the pattern of regular expressions.
- $replacement: A string that specifies the replaced content.
- $string: A string, the original string that needs to be replaced.
- $option (optional): A string that specifies options for regular expressions. The default is "msr", and the following options are available:
- "m": multi-line mode. Make ^ and $ match the beginning and end of the row, respectively.
- "s": single-line mode. Make . match all characters including line breaks.
- "r": Replace all matches instead of just replacing the first match.
Return value: Returns the replaced string, and returns FALSE if an error occurs.
Example:
$str = "Hello, 你好!"; $pattern = "[你好]"; $replacement = "Hi"; $result = mb_ereg_replace($pattern, $replacement, $str); echo $result;
Output:
Hello, Hi!
Notes:
- The mb_ereg_replace() function is case-sensitive. If a case-insensitive replacement is required, use the mb_eregi_replace() function.
- If you need to match multiple patterns for replacement, you can use an array as parameters for $pattern and $replacement, which can replace multiple patterns at once.
- The mb_ereg_replace() function needs to set the correct character encoding, and you can use the mb_regex_encoding() function to set the character encoding.
- In PHP 7, the mb_ereg_replace() function has been deprecated, and it is recommended to use the preg_replace() function instead.