summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Bensmann <lars@almosthappy.de>2014-11-22 18:21:06 +0100
committerLars Bensmann <lars@almosthappy.de>2014-11-22 18:21:06 +0100
commit222dcd48082e355cf593594b83bd6a9bb7a36d09 (patch)
treeae9fa5c2fb75c049ace1a67e0f385196eb8f7943
parent7b1938d070e6526cf6828ff4f2f407d5abdb217d (diff)
Only use mb_detect_encoding() if no charset is set in HTML
-rw-r--r--articleenhancer/xpatharticleenhancer.php21
1 files changed, 19 insertions, 2 deletions
diff --git a/articleenhancer/xpatharticleenhancer.php b/articleenhancer/xpatharticleenhancer.php
index b283786b8..7c91cf13d 100644
--- a/articleenhancer/xpatharticleenhancer.php
+++ b/articleenhancer/xpatharticleenhancer.php
@@ -52,8 +52,25 @@ class XPathArticleEnhancer implements ArticleEnhancer {
if(preg_match($regex, $item->getUrl())) {
$body = $this->getFile($item->getUrl());
- $body = mb_convert_encoding($body, 'HTML-ENTITIES',
- mb_detect_encoding($body));
+
+ // Determine document encoding.
+ // First check if <meta charset="..."> is specified and use that
+ // If this fails look for charset in <meta http-equiv="Content-Type" ...>
+ // As a last resort use mb_detect_encoding()
+ // Use UTF-8 if mb_detect_encoding does not return anything (or the HTML page is messed up)
+ $csregex = "/<meta\s+[^>]*charset\s*=\s*['\"]([^>]*)['\"][^>]*>/i";
+ if(preg_match($csregex, $body, $matches)) {
+ $enc = strtoupper($matches[1]);
+ } else {
+ $ctregex = "/<meta\s+[^>]*http-equiv\s*=\s*['\"]content-type['\"]\s+[^>]*content\s*=\s*['\"][^>]*charset=([^>]*)['\"][^>]*>/i";
+ if(preg_match($ctregex, $body, $matches)) {
+ $enc = strtoupper($matches[1]);
+ } else {
+ $enc = mb_detect_encoding($body);
+ }
+ }
+ $enc = $enc ? $enc : "UTF-8";
+ $body = mb_convert_encoding($body, 'HTML-ENTITIES', $enc);
$dom = new DOMDocument();