将preg_replace()替换为preg_replace_callback()

时间:2014-05-06 15:07:37

标签: php preg-replace preg-replace-callback

$source = preg_replace('/&#(\d+);/me', "utf8_encode(chr(\\1))", $source);

上面的代码给出了不推荐的警告。

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

如何将preg_replace()替换为preg_replace_callback()?

2 个答案:

答案 0 :(得分:4)

请阅读此处的文档http://www.php.net/manual/en/function.preg-replace-callback.php

以下是preg_replace_callback

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

答案 1 :(得分:2)

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