Do not use mb_convert_encoding with HTML-ENTITIES as target encoding

This is deprecated since PHP 8.2:

    Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead

It was used because `DOMDocument`, which uses libxml2 internally, will parse the HTML as ISO-8859-1, unless the document contains an XML encoding declaration or HTML meta tag setting character set.
Since first such element wins, putting the `meta[charset]` up front will ensure the parser uses the correct encoding, even if the document contains incorrect meta tag (e.g. when the document is converted to UTF-8 without also updating the metadata by the software passing it to Readability).

https://stackoverflow.com/a/39148511/160386
This commit is contained in:
Jan Tojnar
2023-03-31 05:26:07 +02:00
parent 23f824a1ce
commit f14428e4c0
2 changed files with 2 additions and 3 deletions
+1 -1
View File
@@ -1426,7 +1426,7 @@ class Readability implements LoggerAwareInterface
unset($tidy);
}
$this->html = mb_convert_encoding((string) $this->html, 'HTML-ENTITIES', 'UTF-8');
$this->html = '<meta charset="utf-8">' . (string) $this->html;
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
$this->dom = (new HTML5())->loadHTML($this->html);