2016-06-01 46 views
1

我有PHP 5.5中的問題:當我使用此代碼:警告:preg_replace_callback():修改/ E不能與更換回調使用

$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_replace ():該/ e修飾符已過時,使用preg_replace_callback代替

我與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); 

它返回警告:

警告:preg_replace_callback():修改/ E不能與更換回調使用

什麼將是實現這一目標的正確的代碼?

+4

的問題是與''e'(修改)'你用'preg_replace_callback一起使用'regex'模式中()'function.Remove來自你的正則表達式的'e'(修飾符)'。所以簡單的代碼看起來像'preg_replace_callback('/&#(\ d +);/m',function($ m){return utf8_encode(chr($ m [1]));},$ source);' –

+0

感謝您的支持,它的工作。 –

回答

0

Narendrasingh Sisodia發表以下內容作爲評論;它應該是一個答案,所以我在這裏將其添加爲社區維基:

的問題是,你是用preg_replace_callback()功能一起使用正則表達式內的e(修正值)。從您的正則表達式中刪除e(修飾符)。

所以,簡單代碼如下:

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