我有一个包含多个标签的字符串。我想获取所有内容并将它们连接到一个有效的结构。例如:<html><body><div>Content</div></body></html>
<html><body><div>Content</div></body></html>
<html><body><div>Content</div></body></html>
<html><body><div>Content</div></body></html>
应该是:
<html>
<body>
<div>Content</div>
<div>Content</div>
<div>Content</div>
</body>
</html>
我当前的代码如下所示:
libxml_use_internal_errors(true);
$newDom = new DOMDocument();
$newBody = "";
$newDom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$bodyTags = $newDom->getElementsByTagName("body");
foreach($bodyTags as $body) {
$newBody .= $newDom->saveHTML($body);
}
$newBody现在包含所有正文标签:
<body><div>Content</div></body>
<body><div>Content</div></body>
<body><div>Content</div></body>
如何只保存每个正文标签的HTML内容?$newBody
编辑:
基于@NigelRen的答案,这是我的解决方案:
libxml_use_internal_errors(true);
$newDom = new DOMDocument();
$newBody = '';
$newDom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$bodyTags = $newDom->getElementsByTagName("body");
foreach($bodyTags as $body) {
foreach ($body->childNodes as $node) {
$newBody .= $newDom->saveHTML($node);
}
}
$newDom = new DOMDocument();
$newDom->loadHTML(mb_convert_encoding($newBody, 'HTML-ENTITIES', 'UTF-8'));
$newBody = $newDom->saveHTML();
ABOUTYOU