'Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback [duplicate]

I have a problem with php 5.5: when i use this code:

$source = preg_replace('/&#(\d+);/me', "utf8_encode(chr(\\1))", $source);
$source = preg_replace('/&#x([a-f0-9]+);/mei', "utf8_encode(chr(0x\\1))", $source);

return error

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

I use with preg_replace_callback:

$source = preg_replace_callback('/&#(\d+);/me', function($m) { return utf8_encode(chr($m[1])); },$source);
$source = preg_replace_callback('/&#x([a-f0-9]+);/mei', function($m) { return utf8_encode(chr("0x".$m[1])); },$source);

it return warning:

Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback

What would be the correct code for achieving this?

php


Solution 1:[1]

Narendrasingh Sisodia posted the below as a comment; it should have been an answer, so I am adding it here as Community Wiki:

The issue is with that e (modifier) that you were using within your regex pattern along with preg_replace_callback() function. Remove that e (modifier) from your regex.

So simply your code looks like:

preg_replace_callback('/&#(\d+);/m', function($m) { return utf8_encode(chr($m[1])); },$source);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1