Ankit Singh
Ankit Singh

Reputation: 1477

How can I exclude the specific html block within body tag by using DOMDocument?

I'm using DOMDocument to get the HTML from a website. I want to get html within the <body></body> and I got it. But inside body here is a <nav>...</nav> block. How can I exclude <nav></nav> block only by using DOMDocument.

Here is my Code:

<!DOCTYPE html>
<head>
    <title>Title Here</title>
<head>
<?php
  $d = new DOMDocument;
  $mock = new DOMDocument;
  $internalErrors = libxml_use_internal_errors(true);
  $d->loadHTML(file_get_contents('http://www.example.com'));
  $body = $d->getElementsByTagName('body')->item(0);
  foreach ($body->childNodes as $child){
      $mock->appendChild($mock->importNode($child, true));
  }
  libxml_use_internal_errors($internalErrors);
  echo $mock->saveHTML(); //<body>.....</body>
?>
</html>

Upvotes: 0

Views: 313

Answers (1)

Md. Saifur Rahman
Md. Saifur Rahman

Reputation: 87

Please look at the accepted answer on this one, PHP DOM: Get NodeValue excluding the child nodes

You can remove 'nav' node just after gathering all child nodes of the body.

Upvotes: 1

Related Questions