Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOMDocument loadHTML error

It's returning

( ! ) Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Misplaced DOCTYPE declaration in Entity, line: 3 in C:\wamp\www\source.php on line 8

The code:

<?php

    $url = "http://www.some-plain-html-website.com";

    $content = file_get_contents($url);

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

?>

For some reason it wont parse $content. If i make content a string like $content = ""; and i parse it by element tag name it works no problem however if i give it the return of file_get_content() method it yields that error.

The documentation for file_get_contents() says it returns a string but for some reason its not working i even tried $content = (string)file_get_contents($url);

Same thing thanks in advance for all the help.

like image 455
Sterling Duchess Avatar asked Oct 16 '25 17:10

Sterling Duchess


1 Answers

In many cases it is advisable to use libxml_use_internal_errors(true); before $dom->loadHTML($content);

This way a warning will not be thrown, and the page is loaded anyway.

like image 61
Kurt Larsen Avatar answered Oct 18 '25 07:10

Kurt Larsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!