码迷,mamicode.com
首页 > 其他好文 > 详细

编码格式,网上看了资料还是乱码,就试下这个吧

时间:2017-01-03 19:00:26      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:set   turn   gb2312   euc   for   encoding   str   编码格式   格式   

  1. /**
  2. * 将非GBK字符集的编码转为GBK
  3. *
  4. * @param mixed $mixed 源数据
  5. *
  6. * @return mixed GBK格式数据
  7. */
  8. function charsetToGBK($mixed)
  9. {
  10. if (is_array($mixed)) {
  11. foreach ($mixed as $k => $v) {
  12. if (is_array($v)) {
  13. $mixed[$k] = charsetToGBK($v);
  14. } else {
  15. $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
  16. if ($encode == ‘UTF-8‘) {
  17. $mixed[$k] = iconv(‘UTF-8‘, ‘GBK‘, $v);
  18. }
  19. }
  20. }
  21. } else {
  22. $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
  23. //var_dump($encode);
  24. if ($encode == ‘UTF-8‘) {
  25. $mixed = iconv(‘UTF-8‘, ‘GBK‘, $mixed);
  26. }
  27. }
  28. return $mixed;
  29. }
  30.  
  31.  
  32. /**
  33. * 将非UTF-8字符集的编码转为UTF-8
  34. *
  35. * @param mixed $mixed 源数据
  36. *
  37. * @return mixed utf-8格式数据
  38. */
  39. function charsetToUTF8($mixed)
  40. {
  41. if (is_array($mixed)) {
  42. foreach ($mixed as $k => $v) {
  43. if (is_array($v)) {
  44. $mixed[$k] = charsetToUTF8($v);
  45. } else {
  46. $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
  47. if ($encode == ‘EUC-CN‘) {
  48. $mixed[$k] = iconv(‘GBK‘, ‘UTF-8‘, $v);
  49. }
  50. }
  51. }
  52. } else {
  53. $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
  54. if ($encode == ‘EUC-CN‘) {
  55. $mixed = iconv(‘GBK‘, ‘UTF-8‘, $mixed);
  56. }
  57. }
  58. return $mixed;
  59. }

编码格式,网上看了资料还是乱码,就试下这个吧

标签:set   turn   gb2312   euc   for   encoding   str   编码格式   格式   

原文地址:http://www.cnblogs.com/sss-justdDoIt/p/6245620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!