Php implements mutual conversion between UTF-8 and GB2312 encoding

Source: Internet
Author: User
Php implements mutual conversion between UTF-8 and GB2312 encoding

  1. /*************************************** *****
  2. *
  3. * Function name: get_utf8_to_gb ($ value)
  4. * Use: utf8 encoded string to gb2312 encoding
  5. * Author: Liu Xianzhong
  6. * Date: 2011-11-09
  7. *
  8. **************************************** ****/
  9. Function get_utf8_to_gb ($ value ){
  10. $ Value_1 = $ value;
  11. $ Value_2 = @ iconv ("UTF-8", "gb2312 // IGNORE", $ value_1); // use @ to resist errors, if a character does not have a corresponding character in the target character set, the subsequent part of the character is ignored. that is, the result string is incomplete and you need to use // IGNORE
  12. $ Value_3 = @ iconv ("gb2312", "UTF-8 // IGNORE", $ value_2 );
  13. If (strlen ($ value_1) = strlen ($ value_3 ))
  14. {
  15. Return $ value_2;
  16. } Else
  17. {
  18. Return $ value_1;
  19. }
  20. }
  21. /*************************************** *****
  22. *
  23. * Function name: get_gb_to_utf8 ($ value)
  24. * Use: gb2312 encoded string to utf8 encoding
  25. * Author: Liu Xianzhong
  26. * Date: 2011-11-09
  27. *
  28. **************************************** ****/
  29. Function get_gb_to_utf8 ($ value ){
  30. $ Value_1 = $ value;
  31. $ Value_2 = @ iconv ("gb2312", "UTF-8 // IGNORE", $ value_1 );
  32. $ Value_3 = @ iconv ("UTF-8", "gb2312 // IGNORE", $ value_2 );
  33. If (strlen ($ value_1) = strlen ($ value_3 ))
  34. {
  35. Return $ value_2;
  36. } Else
  37. {
  38. Return $ value_1;
  39. }
  40. }
  41. ?>

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.