Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

DOMDocument::loadHTML() warns that Tag section, nav or article is invalid

ErrorWarning: DOMDocument::loadHTML(): Tag section invalid

Short 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.

  • Applies to PHP 8.3, PHP 8.4, libxml2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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";
    }
  2. 2Match what you see, then jump to its fix

    1. 1One warning per element, each reading Tag <name> invalidThe HTML 4 parser does not know HTML5 elementsFix
    2. 2Output differs from what a browser shows, or svg and other modern markup comes out mangledYou need real HTML5 parsing, not just quiet warningsFix
    3. 3A warning about an empty string, and libxml_get_errors() stays emptyAn empty string was passed inFix

What does the error look like?

  • Warning: DOMDocument::loadHTML(): Tag section invalid
  • Warning: DOMDocument::loadHTML(): Tag nav invalid
  • Warning: 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 -c

2Fix

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

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.