編碼格式,網上看了資料還是亂碼,就試下這個吧

來源:互聯網
上載者:User

標籤: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. }

編碼格式,網上看了資料還是亂碼,就試下這個吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.