【发布时间】:2020-01-27 21:01:24
【问题描述】:
这里推荐了两种获取 DOMDocument 节点的外部 HTML 的方法:How to return outer html of DOMDocument?
我对为什么他们似乎以不同的方式对待 HTML 实体很感兴趣。
示例:
function outerHTML($node) {
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($node, true));
return $doc->saveHTML();
}
$html = '<p>ACME’s 27” Monitor is $200.</p>';
$dom = new DOMDocument();
@$dom->loadHTML($html);
$el = $dom->getElementsByTagname('p')->item(0);
echo $el->ownerDocument->saveHtml($el) . PHP_EOL;
echo outerHTML($el) . PHP_EOL;
输出:
<p>ACME’s 27” Monitor is $200.</p> <p>ACME’s 27” Monitor is $200.</p>
这两种方法都使用saveHTML(),但由于某种原因,该函数在最终输出中保留了html实体,而使用节点上下文直接调用saveHTML()则没有。谁能解释为什么 - 最好有某种权威参考?
【问题讨论】:
标签: php html domdocument html-entities