PHP mb_ereg_replace_callback() Function Last Updated : 26 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The mb_ereg_replace_callback() function is an inbuilt function in PHP that is used to replace a regular expression match in a string using the callback function. Syntax: mb_ereg_replace_callback( $pattern, $callback,$string,$options): string|false|null Parameters: The function accepts four parameters that are described below. $pattern: A pattern is a regular expression pattern that is used for replacing a string. It must be a valid regular expression pattern.$callback: A callback function is to be called for each match. The function should accept one parameter (an array containing the match information) and return the replacement string.$string: The input string to search in.$options: This is an optional parameter. Possible values for this parameter are: 'm' for multiline, 's' for single line, and 'r' using Unicode rules when matching. Return values: This function is the input string with all matches of the pattern replaced by the result of the callback function, as a string when it is true, & when it is false then it will give an error. The null will be returned when the string is invalid for the current encoding. Example 1: The following program demonstrates the mb_ereg_replace_callback() function. PHP <?php $string = "I love cats and dogs"; $pattern = "[a-z]"; $callback = function ($match) { return mb_strtoupper($match[0]); }; $replaced_string = mb_ereg_replace_callback($pattern, $callback, $string); echo "The string '$string' with matches replaced is '$replaced_string'."; ?> OutputThe string 'I love cats and dogs' with matches replaced is 'I LOVE CATS AND DOGS'. Example 2: The following program demonstrates the mb_ereg_replace_callback() function. PHP <?php $string = "Hello world"; $pattern = "[w]"; $replaced_string = mb_ereg_replace_callback( $pattern, function ($matches) { $first_letter = $matches[0]; if (mb_strtolower($first_letter) == "w") { return mb_strtoupper($first_letter); } else { return $first_letter; } }, $string ); echo "The string '$string' with matches replaced is ' $replaced_string'."; ?> Output: The string 'Hello world' with matches replaced is 'Hello World'. Reference: https://www.php.net/manual/en/function.mb-ereg-replace-callback.php Comment More infoAdvertise with us Next Article PHP | ereg_replace() Function N neeraj3304 Follow Improve Article Tags : PHP PHP-function PHP-Multibyte-String Similar Reads PHP mb_ereg_replace() Function The mb_ereg_replace() is an inbuilt function in PHP that is used to search & replace a string using the regular expression. This function is similar to the preg_match() but works on the multibyte string. Syntax: mb_ereg_replace( $pattern, $replacement, $string, $options = null): string|false|nul 1 min read PHP mb_eregi_replace() Function The mb_eregi_replace() is an inbuilt function in PHP that is used to perform the replacement of regular expression characters. It is a case-insensitive regular expression search and replace with multi-byte character support. Syntax: mb_eregi_replace( string $pattern, string $replacement, string $str 2 min read PHP | ereg_replace() Function The ereg_replace() is an inbuilt function in PHP and is used to search a string pattern in an other string. If pattern is found in the original string then it will replace matching text with a replacement string. You may refer to the article on Regular Expression for basic understanding of pattern m 3 min read PHP mb_ereg_search() Function The mb_ereg_search() is a PHP function used to search & match for a regular expression pattern in a string using multi-byte characters. It is similar to preg_match() but works with multi-byte characters. Syntax: mb_ereg_search(?string $pattern = null, ?string $options = null): boolParameters: Th 2 min read PHP array_replace() Function The array_replace() function is a builtin function in PHP and it takes a list of arrays separated by commas (,) as parameters and replaces all those values of the first array that have same keys in the other arrays. The replacement is done according to the following rules: If a key in the first arra 3 min read PHP mb_ereg_search_regs() Function The mb_ereg_search_regs() function is an inbuilt function in PHP that is used for regular expressions to match a given string. If the match is found, then it will return the matched part as an array. Syntax: mb_ereg_search_regs( ?string $pattern = null, ?string $options = null): array|falseParameter 2 min read Like