preg_replace_callback() is a PHP function that uses regular expressions to find substrings and a PHP callback function to perform the replacement.
preg_replace_callback replaces the older (and insecure) /e
flag in preg_replace, where PHP would eval
the replace string and execute the PHP contained inside. preg_replace_callback
uses a direct function call instead, which negates that risk entirely.
The most common use is where you need to use a regular expression to find strings, but you need a PHP function to transform that string. Consider the below function, which finds instances of Bob and makes them all uppercase. This example passes an anonymous function (PHP >= 5.3) but you can pass the function name instead.
echo preg_replace_callback('/Bob/', function($match) {
return strtoupper($match[0]);
}, 'We like Bob');
// outputs We like BOB