简体   繁体   中英

In php domxpath i want to grab my full footer with html tags and text

I want to grab footer with all the html tags and text in it with domxpath. html:

 <div class="footer">
 <div class="footer-new">
 <div class="footer-additional">
 <div class="add-top">
    some text here
</div>...

i have text links and images in my footer i just want all the html like we get in the source code with all tags . Thanks..

i am trying with something like this :

  $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();
  }

 }

You could just target the parent footer, then iterate the children then use ->saveHTML() and continually add them inside your string container:

$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;

Sample Output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM