在 php domxpath 中,我想用 html 标签和文本获取我的完整页脚
In php domxpath i want to grab my full footer with html tags and text
我想使用 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();
}
}
您可以只定位父页脚,然后迭代子项,然后使用 ->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;
我想使用 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();
}
}
您可以只定位父页脚,然后迭代子项,然后使用 ->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;