我有一个包含html的字符串。 我怎样才能使字符串变得干净,以便除了标签之外的所有内容都被编码? 例如:
$foo = '<div class="link">Here\'s is a link: "<a href="http://www.example.com">Doors & windows</a>'</div>';
我想将其转换为
$out = '<div class="link">Here\'s is a link: "<a href="http://www.example.com">Doors & windows</a>"</div>';
答案 0 :(得分:1)
首先用另一个标记替换括号,调用htmlentities
,然后转换回来。
$html = str_replace("<","***OPENBRACKET***",$html);
$html = str_replace(">","***CLOSEBRACKET***",$html);
$html = htmlentities($html);
$html = str_replace("***OPENBRACKET***","<",$html);
$html = str_replace("***CLOSEBRACKET***",">",$html);
答案 1 :(得分:1)
此代码段显示了一个将加载某些xml的函数(确保至少打开的标签有一个关闭挂件等,否则您将看到/读取一些错误)然后将htmlentities
应用到所有文本节点上。我实际上并不知道你需要什么,但可能会让你开心:
$foo = '<div class="link">Here\'s is a link: <a href="http://www.example.com">Doors & windows</a></div>';
echo text_htmlentities(utf8_encode($foo));
/**
* add htmlentities onto the text-nodes of an
* xml fragment.
*
* @param string $foo xml fragment (utf8)
* @return string
*/
function text_htmlentities($foo) {
$foo = str_replace('&', '&', $foo);
$dom = new DOMDocument;
$dom->loadXml($foo);
$xpath = new DomXpath($dom);
foreach($xpath->query('//text()') as $node) {
$node->nodeValue = htmlentities($node->nodeValue, ENT_QUOTES, 'UTF-8', false);
}
return str_replace('&','&', $dom->saveXml($dom->firstChild));
}
输出:
<div class="link">Here's is a link: <a href="http://www.example.com">Doors & windows</a></div>
答案 2 :(得分:0)
尝试使用html_entity_decode功能