Randy
Randy

Reputation: 1

help with php DOM function

i have got the following code running, but it still sees a link on an exteranl webpage which is not inside the body tag as valid...not sure whats wrong...

$dom = new DOMDocument();
     @$dom->loadHTML($get_webpage);
   @$dom->getElementsByTagName('body');
      $x = new DOMXPath($dom);

     foreach($x->query("//a") as $node)
       {
            if ($node->getAttribute("href") == "http://www.mysite.com/" && $node->getAttribute("rel") == "" && $node->textContent == "mysite")
                    {
              // echo 'valid link on site';
            }

       }

External webpage

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<a href="http://www.mysite.com/"><strong>mysite<strong></a>
<body>
text text
</body>
</html>

Upvotes: 0

Views: 98

Answers (1)

netcoder
netcoder

Reputation: 67735

It's because DOM will rewrite the HTML "properly". If you do saveHTML, you will see that the a tag is now in the body tag. I don't think there is a way to change that behavior.

Side note:

Use this:

libxml_use_internal_errors(true);
$dom->loadHTML($data);

Instead of:

@$dom->loadHTML($data);

It's cleaner.

Upvotes: 1

Related Questions