Maaz Zindani

Loading the index…

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

DOMDocument::loadHTML warns: htmlParseEntityRef: expecting ';' in Entity

ErrorWarning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,

Short answer

loadHTML() warns when the markup contains an ampersand that does not form a complete character reference, most often a raw & inside a URL's query string. Escape the text as & before it reaches the parser, or switch on libxml's internal error handling so the warnings are collected instead of printed.

  • Applies to PHP 8.3, PHP 8.4, ext-dom, ext-libxml
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print every libxml error with its line and column, so you can see which piece of markup triggers each warning.

    <?php
    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    foreach (libxml_get_errors() as $e) {
        printf("line %d col %d: %s", $e->line, $e->column, $e->message);
    }
    libxml_clear_errors();
  2. 2Match what you see, then jump to its fix

    1. 1the reported line contains something like href="page.php?a=1&b=2"An unescaped ampersand in a URL or in textFix
    2. 2several warnings on pages you scrape, while the parsed tree is still usableThe HTML comes from a page you cannot fix, and the warnings are noiseFix

What does the error look like?

  • Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,

Cause 1 of 2

An unescaped ampersand in a URL or in text

1Check

The HTML standard says an attribute value or text must not contain an ambiguous ampersand, which is why & should be written as a reference. Query strings with several parameters are the common source.

grep -n "href=[^ >]*&[a-z]" page.html

2Fix

When you build the HTML in PHP, pass values through htmlspecialchars(), which turns & into &amp;. Use double_encode false if some text is already escaped.

$url  = 'page.php?a=1&b=2';
$html = '<a href="' . htmlspecialchars($url, ENT_QUOTES) . '">link</a>';

3Verify

Run the load again with errors enabled; the entity warning is gone.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

The HTML comes from a page you cannot fix, and the warnings are noise

1Check

The manual says malformed HTML should still load, but loadHTML() may emit E_WARNING errors for bad markup, and that libxml's error functions can handle them.

php -r 'var_dump(libxml_use_internal_errors(true));'

2Fix

Enable internal error handling before loading, clear the buffer afterwards, and decide from the collected errors whether anything matters.

$previous = libxml_use_internal_errors(true);
$doc->loadHTML($html);
$errors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($previous);

3Verify

The page loads without warnings on screen or in the log, and $errors holds what was reported.

php your-script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Escape every dynamic value with htmlspecialchars() when you assemble HTML.
  • Remember that loadHTML() parses with HTML 4 rules and is not a sanitiser, so do not use it to clean untrusted input.
  • Restore the previous libxml error setting after your call so other code is not affected.

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.