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

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

  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. }

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