+ return $content;
+ }
+
+ private function convert_to_utf8($html, $header=null)
+ {
+ $encoding = null;
+ if ($html || $header) {
+ if (is_array($header)) $header = implode("\n", $header);
+ if (!$header || !preg_match_all('/^Content-Type:\s+([^;]+)(?:;\s*charset=["\']?([^;"\'\n]*))?/im', $header, $match, PREG_SET_ORDER)) {
+ // error parsing the response
+ } else {
+ $match = end($match); // get last matched element (in case of redirects)
+ if (isset($match[2])) $encoding = trim($match[2], "\"' \r\n\0\x0B\t");
+ }
+ // TODO: check to see if encoding is supported (can we convert it?)
+ // If it's not, result will be empty string.
+ // For now we'll check for invalid encoding types returned by some sites, e.g. 'none'
+ // Problem URL: http://facta.co.jp/blog/archives/20111026001026.html
+ if (!$encoding || $encoding == 'none') {
+ // search for encoding in HTML - only look at the first 35000 characters
+ $html_head = substr($html, 0, 40000);
+ if (preg_match('/^<\?xml\s+version=(?:"[^"]*"|\'[^\']*\')\s+encoding=("[^"]*"|\'[^\']*\')/s', $html_head, $match)) {
+ $encoding = trim($match[1], '"\'');
+ } elseif (preg_match('/<meta\s+http-equiv=["\']?Content-Type["\']? content=["\'][^;]+;\s*charset=["\']?([^;"\'>]+)/i', $html_head, $match)) {
+ $encoding = trim($match[1]);
+ } elseif (preg_match_all('/<meta\s+([^>]+)>/i', $html_head, $match)) {
+ foreach ($match[1] as $_test) {
+ if (preg_match('/charset=["\']?([^"\']+)/i', $_test, $_m)) {
+ $encoding = trim($_m[1]);
+ break;
+ }
+ }
+ }
+ }
+ if (isset($encoding)) $encoding = trim($encoding);
+ // trim is important here!
+ if (!$encoding || (strtolower($encoding) == 'iso-8859-1')) {
+ // replace MS Word smart qutoes
+ $trans = array();
+ $trans[chr(130)] = '‚'; // Single Low-9 Quotation Mark
+ $trans[chr(131)] = 'ƒ'; // Latin Small Letter F With Hook
+ $trans[chr(132)] = '„'; // Double Low-9 Quotation Mark
+ $trans[chr(133)] = '…'; // Horizontal Ellipsis
+ $trans[chr(134)] = '†'; // Dagger
+ $trans[chr(135)] = '‡'; // Double Dagger
+ $trans[chr(136)] = 'ˆ'; // Modifier Letter Circumflex Accent
+ $trans[chr(137)] = '‰'; // Per Mille Sign
+ $trans[chr(138)] = 'Š'; // Latin Capital Letter S With Caron
+ $trans[chr(139)] = '‹'; // Single Left-Pointing Angle Quotation Mark
+ $trans[chr(140)] = 'Œ'; // Latin Capital Ligature OE
+ $trans[chr(145)] = '‘'; // Left Single Quotation Mark
+ $trans[chr(146)] = '’'; // Right Single Quotation Mark
+ $trans[chr(147)] = '“'; // Left Double Quotation Mark
+ $trans[chr(148)] = '”'; // Right Double Quotation Mark
+ $trans[chr(149)] = '•'; // Bullet
+ $trans[chr(150)] = '–'; // En Dash
+ $trans[chr(151)] = '—'; // Em Dash
+ $trans[chr(152)] = '˜'; // Small Tilde
+ $trans[chr(153)] = '™'; // Trade Mark Sign
+ $trans[chr(154)] = 'š'; // Latin Small Letter S With Caron
+ $trans[chr(155)] = '›'; // Single Right-Pointing Angle Quotation Mark
+ $trans[chr(156)] = 'œ'; // Latin Small Ligature OE
+ $trans[chr(159)] = 'Ÿ'; // Latin Capital Letter Y With Diaeresis
+ $html = strtr($html, $trans);
+ }
+ if (!$encoding) {
+ $encoding = 'utf-8';
+ } else {
+ if (strtolower($encoding) != 'utf-8') {
+ $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8');
+ /*
+ if (function_exists('iconv')) {
+ // iconv appears to handle certain character encodings better than mb_convert_encoding
+ $html = iconv($encoding, 'utf-8', $html);
+ } else {
+ $html = mb_convert_encoding($html, 'utf-8', $encoding);
+ }
+ */
+ }
+ }
+ }
+ return $html;
+ }