Encoding format, read the data on the Internet or garbled, try this bar

Source: Internet
Author: User

  1. /**
  2. * Convert non-GBK character set encoding to GBK
  3. *
  4. * @param mixed $mixed source data
  5. *
  6. * @return Mixed GBK Format data
  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. * Convert non-UTF-8 character set encoding to UTF-8
  32. *
  33. * @param mixed $mixed source data
  34. *
  35. * @return Mixed Utf-8 Format data
  36. */
  37. function charsetToUTF8($mixed)
  38. {
  39. if (is_array($mixed)) {
  40. foreach ($mixed as $k = = $v) {
  41. if (is_array($v)) {
  42. $mixed[$k] = charsetToUTF8($v);
  43. } else {
  44. $encode = mb_detect_encoding($v, array(' ASCII ', ' UTF-8 ', ' GB2312 ', ' GBK ', ' BIG5 ');
  45. if ($encode = = ' euc-cn ') {
  46. $mixed[$k] = iconv(' GBK ', ' UTF-8 ', $v);
  47. }
  48. }
  49. }
  50. } else {
  51. $encode = mb_detect_encoding($mixed, array(' ASCII ', ' UTF-8 ', ' GB2312 ', ' GBK ', ' BIG5 ');
  52. if ($encode = = ' euc-cn ') {
  53. $mixed = iconv(' GBK ', ' UTF-8 ', $mixed);
  54. }
  55. }
  56. return $mixed;
  57. }

Encoding format, read the data on the Internet or garbled, try this bar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.