Function name: mb_ereg_replace_callback()
Applicable version: PHP 4 >= 4.2.0, PHP 5, PHP 7
Function description: The mb_ereg_replace_callback() function is a replacement function for multi-byte regular expressions, used to search for content matching the regular expression in a string and replace it with a callback function.
Syntax: mb_ereg_replace_callback(string $pattern, callable $callback, string $subject [, string $option = "msri"]) : string|false
parameter:
Return value: Returns the replaced string, and returns false if an error occurs.
Example:
// 替换字符串中的数字为其平方值function square($matches) { return $matches[0] * $matches[0]; } $string = "1 2 3 4 5"; $pattern = "/\d+/u"; $result = mb_ereg_replace_callback($pattern, "square", $string); echo $result; // 输出:1 4 9 16 25
In the above example, we define a callback function called square
to calculate the square value of the matched number. We then use the mb_ereg_replace_callback()
function to search for the number in the string $string
and replace it with a square value. Finally, we output the replaced string to the screen and we get the expected result: 1 4 9 16 25
.