mb_ereg_replace_callback()

mb_ereg_replace_callback() — Perform a regular expression search and replace with multibyte support using a callback.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.4.1, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

mb_ereg_replace_callback(string $pattern, callable $callback, string $string, ?string $options = null): string|false|null

Parameters

ParameterTypeWhat it takes
$patternstringThe regular expression pattern.
$callbackcallableA callback that will be called and passed an array of matched elements in the string string. The callback should return the replacement string.
$stringstringThe string being checked.
$options?stringThe search option. See mb_regex_set_options() for explanation.

Return value

The resultant string on success, or false on error. If string is not valid for the current encoding, null is returned.

Example

A runnable snippet:

// this text was used in 2002 // we want to get this up to date for 2003 $text = "April fools day is 04/01/2002\n"; $text.= "Last christmas was 12/24/2001\n"; // the callback function function next_year($matches) { // as usual: $matches[0] is the complete match // $matches[1] the match for the first subpattern // enclosed in '(...)' and so on return $matches[1].($matches[2]+1); } echo mb_ereg_replace_callback( "(\d{2}/\d{2}/)(\d{4})", "next_year", $text);

Output:

April fools day is 04/01/2003 Last christmas was 12/24/2002

Changelog

VersionChange
8.0.0options is nullable now.
7.1.0The function checks whether string is valid for the current encoding.

See also

mb_regex_encoding(), mb_ereg_replace()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mb_ereg_replace_callback() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.