PHP DOMDocument loadHTML Not Encoding UTF-8 Correctly
When parsing HTML strings containing non-ASCII characters, DOMDocument often behaves unexpectedly if the encoding context is not explicitly defined. A common symptom involves Japanese (or any other multi-byte character set) text appearing as garbled mojibakeâsuch as ã¤ãªãâupon retrieval via saveHTML(), even though the original string or a direct echo displays correctly.
This issue stems from PHP's default assumption regarding input encoding when using loadHTML(). The DOM parser requires an explicit declaration of the document's character set before it begins parsing. Without this, PHP assumes an ISO-8859-1 (Latin-1) encoding by default. When you pass a UTF-8 string containing Japanese characters without telling the parser about the encoding, it interprets the multi-byte UTF-8 sequences as individual Latin-1 bytes. This results in the corrupted output observed when serializing the DOM back to HTML.
Explicitly Setting the Encoding
To resolve this, you must tell DOMDocument what encoding the input string uses immediately after instantiation or before calling loadHTML(). In PHP 5.3 and later, the most robust approach is to set the internal encoding property of the DOMDocument instance. This ensures that the parser treats the input stream as UTF-8 and preserves the byte sequences correctly during parsing and serialization.
$profile = '<div lang="ja"><p>ã¤ãªãã¤å·ã·ã«ã´ã«ã¦ãã¢ã¤ã«ã©ã³ãç³»ã®å®¶åºã«ã</p></div>';
$dom = new DOMDocument();
// Explicitly set the internal encoding to UTF-8
$dom->encoding = 'UTF-8';
// Alternatively, use loadHTML with the encoding parameter (PHP 5.3+)
// $dom->loadHTML($profile, LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED);
$dom->loadHTML($profile, LIBXML_NOBLOSOM);
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
echo $dom->saveHTML($div);
}
By setting $dom->encoding = 'UTF-8', the parser correctly interprets the input string's byte order. When you subsequently call saveHTML(), it outputs the content in the specified encoding, preserving the original Japanese characters without corruption.
Handling External Scripts and Stylesheets
Another critical consideration arises when your HTML document references external scripts or stylesheets. If these resources are loaded from a different domain or if the browser's Content Security Policy restricts cross-origin requests, they may fail to load unless the server explicitly declares the correct MIME type and character set in the HTTP headers.
For example, ensure that the response header includes:
header('Content-Type: text/html; charset=utf-8');
Without this declaration, browsers might default to ISO-8859-1 or another encoding, leading to inconsistent rendering across different environments. When building complex applications, always verify that your server configuration aligns with the expected encoding of your content.
Trade-offs and Best Practices
Setting $dom->encoding = 'UTF-8' is a straightforward fix for local string parsing, but it does not address issues related to external resource loading or cross-origin requests. If you are working with real-world HTML documents fetched from remote URLs, consider using loadHTMLFile() combined with proper HTTP headers and the LIBXML_NOBLOSOM flag to avoid unintended modifications to the document structure.
Additionally, be mindful of PHP versions. While this behavior has been consistent since PHP 5.3, newer versions introduce additional flags and methods for handling encoding more gracefully. Always test your code against multiple PHP versions if your application runs in a heterogeneous environment.
In summary, the key takeaway is that DOMDocument requires an explicit encoding declaration when dealing with non-ASCII characters. By setting the encoding to UTF-8 before parsing, you ensure consistent behavior across different platforms and avoid subtle bugs related to character corruption.
