From 2cb87aca330bf773da47c1a4c8101a00e83b4e7a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 22 Sep 2020 20:35:08 -0400 Subject: [PATCH] Improve charset detection in Util\ParseUrl - Pages with charset meta tag weren't properly decoded --- src/Util/ParseUrl.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 01ad79d4f1..bb3ebbc10b 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -201,9 +201,18 @@ class ParseUrl } } - // Fetch the first mentioned charset. Can be in body or header $charset = ''; - if (preg_match('/charset=(.*?)[\'"\s\n]/', $header, $matches)) { + // Look for a charset, first in headers + // Expected form: Content-Type: text/html; charset=ISO-8859-4 + if (preg_match('/charset=(.+?)\s/', $header, $matches)) { + $charset = trim(trim(trim(array_pop($matches)), ';,')); + } + + // Then in body that gets precedence + // Expected forms: + // - + // - + if (preg_match('/charset=["\']?([^\'"]*?)[\'"]/', $body, $matches)) { $charset = trim(trim(trim(array_pop($matches)), ';,')); }