Merge pull request #9263 from MrPetovan/bug/parse-url-charset

Improve charset detection in Util\ParseUrl
This commit is contained in:
Michael Vogel 2020-09-23 05:13:50 +02:00 committed by GitHub
commit 9c93240770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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:
// - <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
// - <meta charset="utf-8">
if (preg_match('/charset=["\']?([^\'"]*?)[\'"]/', $body, $matches)) {
$charset = trim(trim(trim(array_pop($matches)), ';,'));
}