欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php字符串编码转换函数 可以自动识别原编码

程序员文章站 2022-05-17 16:37:37
...
  1. /**
  2. * 数据编码转换
  3. * @param array/string $data 数组
  4. * @param string $output 转换后的编码
  5. */
  6. function array_iconv($data,$output = 'utf-8') {
  7. $encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');
  8. $encoded = mb_detect_encoding($data, $encode_arr);//自动判断编码
  9. if (!is_array($data)) {
  10. return mb_convert_encoding($data, $output, $encoded);
  11. } // bbs.it-home.org
  12. else {
  13. foreach ($data as $key=>$val) {
  14. if(is_array($val)) {
  15. $data[$key] = array_iconv($val, $input, $output);
  16. } else {
  17. $data[$key] = mb_convert_encoding($data, $output, $encoded);
  18. }
  19. }
  20. return $data;
  21. }
  22. }
复制代码