Current Location: Home> Function Categories> mb_ereg_replace_callback

mb_ereg_replace_callback

Perform regular expression search using callbacks and use multibyte support for replacement
Name:mb_ereg_replace_callback
Category:Multi-byte string
Programming Language:php
One-line Description:A multi-byte regular expression replacement function that searches for content matching the regular expression in a string and uses a callback function to replace it

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:

  • $pattern: The regular expression pattern to search for.
  • $callback: callback function, used to replace the matching content.
  • $subject: The string to search and replace.
  • $option (optional): Optional parameter to set regular expression options. The default is "msri", which corresponds to the following options:
    • m: Multi-line pattern, making ^ and $ match the start and end positions of each row.
    • s: Single-line pattern to make . match any character (including newlines).
    • r: The replaced string is returned as the result, rather than the replaced string.
    • i: case insensitive matching.

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 .

Similar Functions
Popular Articles