不推荐使用iconv_set_encoding()破坏了所需的功能

时间:2017-01-06 10:59:44

标签: php character-encoding

我曾经有过这个很棒的小函数来将奇怪的ISO-8895-15字符转换为字符串中的UTF-8,因此它不会破坏RSS / Atom提要

function convertCharset($contents) {
  iconv_set_encoding("internal_encoding", "ISO-8859-15");
  iconv_set_encoding("output_encoding", "ISO-8859-1");
  ob_start("ob_iconv_handler");
  echo $contents;
  $contents = ob_get_clean();
  $contents = strtr($contents, array(
    "\x80" => "e",
    "\x81" => " ",
    "\x82" => "'",
    "\x83" => 'f',
    "\x84" => '"',
    "\x85" => "...",
    "\x86" => "+",
    "\x87" => "#",
    "\x88" => "^",
    "\x89" => "0/00",
    "\x8A" => "S",
    "\x8B" => "<",
    "\x8C" => "OE",
    "\x8D" => " ",
    "\x8E" => "Z",
    "\x8F" => " ",
    "\x90" => " ",
    "\x91" => "`",
    "\x92" => "'",
    "\x93" => '"',
    "\x94" => '"',
    "\x95" => "*",
    "\x96" => "-",
    "\x97" => "&mdash;",
    "\x98" => "~",
    "\x99" => "(TM)",
    "\x9A" => "s",
    "\x9B" => ">",
    "\x9C" => "oe",
    "\x9D" => " ",
    "\x9E" => "z",
    "\x9F" => "Y")
  );
  return str_replace('iso-8859-1', 'utf-8', utf8_encode($contents));

}

现在,在iconv.internal_encoding,iconv.output_encoding在PHP =&gt;中弃用后,它已经崩溃了5.6 在php.net中有人说我应该使用ini_set(&#39; default_charset&#39;,&#39; UTF-8&#39;);但我不认为这在这个功能中不起作用 我该怎么做才能让这个功能再次起作用

1 个答案:

答案 0 :(得分:0)

试试这个。

if (PHP_VERSION_ID < 50600) {
    iconv_set_encoding('input_encoding', 'UTF-8');
    iconv_set_encoding('output_encoding', 'UTF-8');
    iconv_set_encoding('internal_encoding', 'UTF-8');
} else {
    ini_set('default_charset', 'UTF-8');
}

由于