Mr. Lavalamp
Mr. Lavalamp

Reputation: 1888

DOMDocument not returning results (PHP)

I am trying to find all line items with a class of "menu_item" and it's not turning anything up. The page most certainly has 'content ' scattered throughout, so that's not the problem. Thanks for any help you can offer!

$url = "http://www.examplesite.com";
$doc = new DOMDocument;

$html = file_get_contents($url);
$doc->loadHTML($html);
$xPath = new DOMXPath($doc);

$results = $xPath->query('//li[@class="menu_item"]');
print_r($results);

The printing only returns this: 'DOMNodeList Object ( )'

Upvotes: 1

Views: 51

Answers (1)

anubhava
anubhava

Reputation: 784868

That is correct behavior since $xpath->query method returns an object of type DOMNodeList. Try iterating $results object like this:

if ($results) {
   for ($i=0; $i < $results->length; $i++) {
       $node = $results->item($i);
       var_dump($node);
   }
}

Upvotes: 1

Related Questions