tags:

views:

1488

answers:

4
$html = file_get_contents("http://www.somesite.com/");

$dom = new DOMDocument();
$dom->loadHTML($html);

echo $dom;

throws

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,
Catchable fatal error: Object of class DOMDocument could not be converted to string in test.php on line 10
+2  A: 

$dom is an object, not a string, and you can't just echo $dom. What were you expecting to see?

Jim Garrison
+3  A: 

The reason for your fatal error is DOMDocument does not have a __toString() method and thus can not be echo'ed.

You're probably looking for

echo $dom->saveHTML();
Mike B
+1  A: 

There are 2 errors: the second is because $dom is no string but an object and thus cannot be "echoed". The first error is a warning from loadHTML, caused by invalid syntax of the html document to load (probably a & used as parameter separator and not masked as entity with &).

You ignore and supress this error message (not the error, just the message!) by calling the function with the error control operator "@" (http://www.php.net/manual/en/language.operators.errorcontrol.php )

$dom->@loadHTML($html);
A: 

$dom->@loadHTML($html); This is incorrect use this

@$dom->loadHTML($html);

Maanas