作者:爱中华爱美丽 | 来源:互联网 | 2023-01-21 17:20
我正在写一个RSS到JSON解析器,作为其中的一部分,我需要在description标签内找到的任何标签上使用htmlentities().目前,我正在尝试使用preg_repla
我正在写一个RSS到JSON解析器,作为其中的一部分,我需要在description标签内找到的任何标签上使用htmlentities().目前,我正在尝试使用preg_replace(),但我正在努力使用它.我当前的(非工作)代码如下所示:
$pattern[0] = "/\(.*?)\<\/description\>/is";
$replace[0] = ''.htmlentities("$1").'';
$rawFeed = preg_replace($pattern, $replace, $rawFeed);
如果您对此也有更优雅的解决方案,请分享.谢谢.
解决方法:
简单.使用preg_replace_callback:
function _handle_match($match)
{
return '' . htmlentities($match[1]) . '';
}
$pattern = "/\(.*?)\<\/description\>/is";
$rawFeed = preg_replace_callback($pattern, '_handle_match', $rawFeed);
它接受任何回调类型,也接受类中的方法.