DOMDocument::loadHTML() warns that Tag section, nav or article is invalid
Warning: DOMDocument::loadHTML(): Tag section invalidShort answer
DOMDocument::loadHTML() uses an HTML 4 parser, so HTML5 elements such as section, nav or article are unknown to it and each one raises a warning. The page still loads. Collect the warnings with libxml_use_internal_errors(true), or on PHP 8.4 switch to Dom\HTMLDocument, which parses HTML5.
Fix it fast
1Find out which cause you have
Load the markup with libxml's own error buffer switched on, then print what it collected. This shows whether every message is an unknown-tag message or a real syntax problem.
<?php libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTML($html); foreach (libxml_get_errors() as $e) { echo trim($e->message), ' (line ', $e->line, ")\n"; }2Match what you see, then jump to its fix
- 1
One warning per element, each reading Tag <name> invalidThe HTML 4 parser does not know HTML5 elementsFix - 2
Output differs from what a browser shows, or svg and other modern markup comes out mangledYou need real HTML5 parsing, not just quiet warningsFix - 3
A warning about an empty string, and libxml_get_errors() stays emptyAn empty string was passed inFix
- 1
What does the error look like?
Warning: DOMDocument::loadHTML(): Tag section invalidWarning: DOMDocument::loadHTML(): Tag nav invalidWarning: DOMDocument::loadHTML(): Tag article invalid
Cause 1 of 3
The HTML 4 parser does not know HTML5 elements
1Check
The warning comes from libxml2, which prints Tag %s invalid when it cannot find the element name in its HTML 4 table. The manual states that loadHTML() uses an HTML 4 parser. Elements like section, nav, header, footer, main and article trigger it.
grep -o -E '<(section|nav|article|header|footer|main|aside)' page.html | sort | uniq -c2Fix
Turn on libxml's internal error handling before loading, read or discard the messages, then clear the buffer and restore the previous setting. The document is still built, and the unknown elements stay in the tree.
<?php
$previous = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
libxml_clear_errors();
libxml_use_internal_errors($previous);3Verify
Run the script. No warnings print, and the HTML5 element is still found.
<?php
echo $doc->getElementsByTagName('section')->length;Confirmed by php.net
Cause 2 of 3
You need real HTML5 parsing, not just quiet warnings
1Check
Hiding the warnings does not change how the parser works. The manual says loadHTML() applies HTML 4 rules and cannot be used safely for sanitising, and points to Dom\HTMLDocument for modern HTML. That class exists from PHP 8.4.
php -r 'echo PHP_VERSION, PHP_EOL, class_exists("Dom\\HTMLDocument") ? "HTMLDocument available" : "upgrade to 8.4";'2Fix
On PHP 8.4 or later, parse with Dom\HTMLDocument::createFromString(). It follows the HTML5 rules and raises no unknown-tag warnings.
<?php
$doc = Dom\HTMLDocument::createFromString(
'<!DOCTYPE html><html><body><main><article><h1>Hello</h1></article></main></body></html>'
);
echo $doc->saveHtml();3Verify
The output keeps main and article, and the run prints no warnings.
php page.phpConfirmed by php.net
Cause 3 of 3
An empty string was passed in
1Check
The manual says the empty-string warning is not generated by libxml, so libxml_use_internal_errors() cannot hide it. Check that the variable holds content before the call.
<?php
var_dump(strlen($html));2Fix
Test for an empty string first and skip the load, or handle the missing content where it is fetched.
<?php
if (trim($html) !== '') {
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
libxml_clear_errors();
}3Verify
Run with an empty and a filled string. Only the filled one is parsed, and no warning appears.
php page.phpConfirmed by php.net
How do I stop it coming back?
- Wrap every loadHTML() call in libxml_use_internal_errors() and clear the buffer after reading it, so long-running workers do not collect old messages.
- Do not use loadHTML() output as a sanitiser. Use a purpose-built HTML sanitiser instead.
- On PHP 8.4 or later, prefer Dom\HTMLDocument for pages written in modern HTML.
- Log the collected libxml messages in development so real syntax problems are not hidden with the unknown-tag noise.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix