我得到了身体内容,但没有html标签(它被清理)在体内。我需要身体内的所有html标签。我想在代码上更改什么?
$doc = new DOMDocument();
@$doc->loadHTMLFile($myURL);
$elements2 = $doc->getElementsByTagName('body');
foreach ($elements2 as $el2) {
echo $el2->nodeValue, PHP_EOL;
echo "<br/>";
}
答案 0 :(得分:1)
您需要将body
子节点保存为HTML。我建议使用Xpath来获取节点,这避免了外部循环:
$html = <<<'HTML'
<html>
<body>
Foo
<p>Bar</p>
</body>
</html>
HTML;
$document = new DOMDocument();
$document->loadHtml($html);
$xpath = new DOMXpath($document);
$result = '';
foreach ($xpath->evaluate('//body/node()') as $node) {
$result .= $document->saveHtml($node);
}
var_dump($result);
输出:
string(29) "
Foo
<p>Bar</p>
"