不推荐使用iconv_set_encoding()破坏所需的功能 [英] iconv_set_encoding() deprecated broke needed function

查看:59
本文介绍了不推荐使用iconv_set_encoding()破坏所需的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经有一个很好的小功能,可以将字符串中奇怪的ISO-8895-15字符转换为UTF-8,这样就不会破坏RSS/Atom提要

I used to have this nice little function to translate weird ISO-8895-15 characters to UTF-8 in a string so it wont break RSS/Atom feeds

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中已弃用之后,它已损坏=> 5.6在php.net中,有人说我应该使用ini_set('default_charset','UTF-8');但我认为这不能在此功能中起作用我应该怎么做才能使此功能再次起作用

now it's broken after iconv.internal_encoding , iconv.output_encoding are deprecated in PHP => 5.6 in php.net someone said that I should use ini_set('default_charset', 'UTF-8'); but I don't think that would not work in this function What should I do to make this function work again

推荐答案

尝试一下.

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');
}

谢谢

这篇关于不推荐使用iconv_set_encoding()破坏所需的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆