preg_replace to preg_replace_callback

时间:2017-01-18 09:50:06

标签: php preg-replace php-7

我刚升级到PHP7并收到此警告:

警告:preg_replace():不再支持/ e修饰符,请在第322行使用preg_replace_callback

如何将preg_replace(以下代码中的第二个preg_replace)替换为preg_replace_callback?这是我不太了解的第三方代码。我知道一些PHP,并试图寻找解决方案,但我无法理解它。

// Remove HTML Entities //
$string = preg_replace('/&(#?x?[0-9a-z]+)?;/', '', $string); 

$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
             "'<[\/\!]*?[^<>]*?>'si",           // Strip out HTML tags
             "'([\r\n])[\s]+'",                 // Strip out white space
             "'&(quot|#34);'i",                 // Replace HTML entities
             "'&(amp|#38);'i",
             "'&(lt|#60);'i",
             "'&(gt|#62);'i",
             "'&(nbsp|#160);'i",
             "'&(iexcl|#161);'i",
             "'&(cent|#162);'i",
             "'&(pound|#163);'i",
             "'&(copy|#169);'i",
             "'&#(\d+);'e");        

if ($search && $string){
    return preg_replace($search, ' ', $string);        // Line 322, the line that I need to fix
} else {
    return false;
}

1 个答案:

答案 0 :(得分:1)

/ e修饰符评估替换字符串中的PHP代码。

因为您的代码仅使用空格替换找到的字符串,所以修改器完全没用,您只需删除行e中的"'&#(\d+);'e");

您可以在此页面上找到更多信息:PHP Pattern Modifiers

相关问题