我想用domxpath抓取包含所有html标签和文本的页脚。 HTML:
<div class="footer">
<div class="footer-new">
<div class="footer-additional">
<div class="add-top">
some text here
</div>...
我的页脚中有文字链接和图片 我只想要所有的html,就像我们在源代码中获得所有标签一样。 感谢..
我正在尝试这样的事情:
$dom = new DOMDocument('1.0');
$dom->loadHTML($input);
$xpath = new DOMXPath($dom);
$tags=$xpath->query('//div[contains(@class,"footer")]');
foreach ($tags as $tag) {
$innerHTML = '';
$children = $tag->childNodes;
foreach ($children as $child) {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($child,true));
$innerHTML .= $tmp_doc->saveHTML();
}
}
答案 0 :(得分:1)
你可以只定位父页脚,然后迭代孩子然后使用->saveHTML()
并不断将它们添加到你的字符串容器中:
$dom = new DOMDocument('1.0');
$dom->loadHTML($input);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[contains(@class,"footer")]');
$innerHTML = '';
if($tags->length > 0) { // if found
foreach($tags->item(0)->childNodes as $c) {
$innerHTML .= $dom->saveHTML($c);
}
}
echo $innerHTML;