我正在写一个RSS
to JSON parser
作为其中的一部分,我需要使用htmlentities()
在描述标签内找到的任何标签上。目前,我正在尝试使用preg_replace()
,但我有点挣扎。我当前的(非工作)代码如下所示:
$pattern[0] = "/\<description\>(.*?)\<\/description\>/is";
$replace[0] = '<description>'.htmlentities("$1").'</description>';
$rawFeed = preg_replace($pattern, $replace, $rawFeed);
如果您对此也有更优雅的解决方案,请分享。谢谢。
简单的。使用preg_replace_callback
:
function _handle_match($match)
{
return '<description>' . htmlentities($match[1]) . '</description>';
}
$pattern = "/\<description\>(.*?)\<\/description\>/is";
$rawFeed = preg_replace_callback($pattern, '_handle_match', $rawFeed);
它接受任何回调类型,类中的方法也是如此。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)