reggie
reggie

Reputation: 3674

How to output DOMDocuments?

Maybe I am missing something... but the DOM Object is empty in this code:

$input = file_get_contents('http://www.google.com/');
$doc = new DOMDocument();
@$doc->loadHTML($input); //supress errors on invalid html!
var_dump($doc);
die();

I really don't know what could be wrong with that code. I have verified that $input is actually filled with the html contents of the web page.

The output is: object(DOMDocument)#3 (0) { }

I don't understand why...

Upvotes: 3

Views: 7280

Answers (3)

bobince
bobince

Reputation: 536379

The output is: object(DOMDocument)#3 (0) { }

Yes. That's what a var_dumped DOMDocument looks like.

If you want to look at the HTML representation of the content inside the document, saveHTML() on it. That spits out a cleaned up version of the HTML on Google's home page for me.

Upvotes: 2

salathe
salathe

Reputation: 51950

This is expected behaviour. To see the HTML, use DOMDocument::saveHTML() (or saveXML()).

Upvotes: 6

Pramendra Gupta
Pramendra Gupta

Reputation: 14873

Try this

$input = file_get_contents('http://www.google.com/');
$doc = new DOMDocument();
$test=@$doc->loadHTML($input); //supress errors on invalid html!
var_dump($test);
die();
//output
//bool(true)
?>

or try

$input = file_get_contents('http://www.google.com/');

$buffer = ob_get_clean();
$tidy = new tidy();
$input = $tidy->repairString($input);

$doc = new DOMDocument();
@$doc->loadHTML($input); //supress errors on invalid html!
var_dump($doc);
die();

Upvotes: 0

Related Questions