Replace deprecated preg_replace /e with preg_replace_callback [duplicate]

You can use an anonymous function to pass the matches to your function: $result = preg_replace_callback( “/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/isU”, function($m) { return CallFunction($m[1], $m[2], $m[3], $m[4], $m[5]); }, $result ); Apart from being faster, this will also properly handle double quotes in your string. Your current code using /e would convert a double quote ” into \”.

Replace preg_replace() e modifier with preg_replace_callback

In a regular expression, you can “capture” parts of the matched string with (brackets); in this case, you are capturing the (^|_) and ([a-z]) parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string. The /e modifier takes a replacement string, … Read more