Skip to content

Commit 18ea6ad

Browse files
authored
Merge pull request #265 from CristianoErardt/improve-encoding-detection
add detect ISO-8859-1 and convert to UTF-8
2 parents 038265b + bb6a505 commit 18ea6ad

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Http/Response.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ public function getHtmlContent()
6262

6363
$this->htmlContent = new DOMDocument();
6464

65-
if (mb_detect_encoding($content) === 'UTF-8') {
65+
if (mb_detect_encoding($content, 'UTF-8', true) === 'UTF-8') {
6666
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
67+
$content = preg_replace(
68+
'/<head[^>]*>/',
69+
'<head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">',
70+
$content
71+
);
72+
} elseif (mb_detect_encoding($content, 'ISO-8859-1', true) === 'ISO-8859-1') {
73+
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'ISO-8859-1');
6774
$content = preg_replace('/<head[^>]*>/', '<head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">', $content);
6875
}
6976

0 commit comments

Comments
 (0)