frenesim
frenesim

Reputation: 703

DomNodeList length return 0

I'm digging a solution for the last 5 hours and I can't believe that i'm asking my first question here for such a simple problem, so the code is:

$urlContents = file_get_contents('http://www.google.com'); 
$dom = new DOMDocument();
$contents = $dom->loadHTMLFile($urlContents);    //$contents = 'source code from www-google.com'
$divsInDomDoc = $dom->getElementsByTagName('div');  // $divsInDomDoc becames a DomNodeList with length = 0
$nNodes = $divsInDomDoc->length  // using this to check length

Please somebody help!

Upvotes: 1

Views: 4433

Answers (1)

nickb
nickb

Reputation: 59709

In addition to Tim Cooper's change, you also need to use loadHTML() instead of loadHTMLFile(), since you already have the contents of the site in a string. loadHTMLFile() is for reading a file.

$contents = $dom->loadHTML($urlContents);  

Upvotes: 1

Related Questions