Php 警告:preg_replace_callback():修饰符/e不能与替换回调一起使用

Php 警告:preg_replace_callback():修饰符/e不能与替换回调一起使用,php,Php,我对php 5.5有一个问题:当我使用此代码时: $source = preg_replace('/&#(\d+);/me', "utf8_encode(chr(\\1))", $source); $source = preg_replace('/&#x([a-f0-9]+);/mei', "utf8_encode(chr(0x\\1))", $source); 返回错误 不推荐使用:preg\u replace():不推荐使用/e修饰符,请改用preg\u replace\u

我对php 5.5有一个问题:当我使用此代码时:

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

不推荐使用:preg\u replace():不推荐使用/e修饰符,请改用preg\u replace\u回调

我与preg_replace_回调一起使用:

$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);
它返回警告:

警告:preg_replace_callback():修饰符/e不能与替换回调一起使用

实现这一目标的正确代码是什么?

将以下内容作为评论发布;这应该是一个答案,因此我将其添加为社区维基:


问题在于您在regex模式中与
preg\u replace\u callback()
函数一起使用的
e
(修饰符)。从正则表达式中删除
e
(修饰符)

因此,您的代码看起来很简单:

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

问题在于您在
regex
模式中使用的
e
(修饰符)
以及
preg\u replace\u callback()
函数。请从regex中删除该
e
(修饰符)
。因此,您的代码看起来就像是
preg_replace_回调('/&#(\d+);/m',函数($m){return utf8_encode(chr($m[1]));},$source)谢谢你的支持,它成功了。