DOMDocument::loadHTML warns: htmlParseEntityRef: expecting ';' in Entity
Warning: 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.
Fix it fast
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();2Match what you see, then jump to its fix
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.html2Fix
When you build the HTML in PHP, pass values through htmlspecialchars(), which turns & into &. 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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
DOMDocument::loadHTML() warns that Tag section, nav or article is invalid
Warning: DOMDocument::loadHTML(): Tag nav invalid
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.
3 causes, each with a fixBack 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 fix