DOMDocument::loadHTML error
Solution 1:
Header, Nav and Section are elements from HTML5. Because HTML5 developers felt it is too difficult to remember Public and System Identifiers, the DocType declaration is just:
<!DOCTYPE html>
In other words, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD and that doesnt contain those elements, hence the Warnings.
To surpress the Warnings, put
libxml_use_internal_errors(true);
before the call to loadHTML
and
libxml_use_internal_errors(false);
after it.
An alternative would be to use https://github.com/html5lib/html5lib-php.
Solution 2:
With a DOMDocument object, you should be able to place an @ before the load method in order to SUPPRESS all WARNINGS.
$dom = new DOMDocument;
@$dom->loadHTML($source);
And carry on.
Solution 3:
HTML5 elements are still not supported, but you can silence libxml errors completely with the $options
parameter.
Just set
$doc = new DOMDocument();
$doc->loadHTMLFile("html5.html", LIBXML_NOERROR);
This option is preferred over @
which silences PHP errors.
But be careful, libxml is very forgiving and it will parse a broken HTML document. If you silence libxml errors you might not even be aware that the HTML is malformed.