How to output DOMDocuments?

Maybe something is missing for me ... 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 this code. I confirmed that $ input is actually populated with the html content of the webpage.

Output: object (DOMDocument) # 3 (0) {}

I do not understand why...

+3
source share
3 answers

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

+6
source

Output: object (DOMDocument) # 3 (0) {}

Yes. It looks like var_dumped DOMDocument.

HTML- , saveHTML() . HTML Google .

+2

  

$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();
0
source

Source: https://habr.com/ru/post/1766545/


All Articles