PHP Unicode decoding tool (Unicode encoding Converter)

Source: Internet
Author: User
Tags php unicode
  1. Unicode Encoding decoding transformation
  2. function Unicode_encode ($name)
  3. {
  4. $name = Iconv (' UTF-8 ', ' UCS-2 ', $name);
  5. $len = strlen ($name);
  6. $str = ";
  7. for ($i = 0; $i < $len-1; $i = $i + 2)
  8. {
  9. $c = $name [$i];
  10. $c 2 = $name [$i + 1];
  11. if (ord ($c) > 0)
  12. {//two bytes of text
  13. $str. = ' \u '. Base_convert (Ord ($c), ten, 0). Str_pad (Base_convert (Ord ($c 2), 2,, str_pad_left);
  14. }
  15. Else
  16. {
  17. $str. = $c 2;
  18. }
  19. }
  20. return $str;
  21. }
  22. Decoding Unicode-encoded content
  23. function Unicode_decode ($name)
  24. {//bbs.it-home.org
  25. Convert encoding to convert Unicode encoding into a utf-8 encoding that can be browsed
  26. $pattern = '/([\w]+) | (\\\u ([\w]{4}))/I ';
  27. Preg_match_all ($pattern, $name, $matches);
  28. if (!empty ($matches))
  29. {
  30. $name = ";
  31. for ($j = 0; $j < count ($matches [0]); $j + +)
  32. {
  33. $str = $matches [0][$j];
  34. if (Strpos ($str, ' \\u ') = = = 0)
  35. {
  36. $code = Base_convert (substr ($STR, 2, 2), 16, 10);
  37. $code 2 = Base_convert (substr ($STR, 4), 16, 10);
  38. $c = Chr ($code). Chr ($code 2);
  39. $c = Iconv (' UCS-2 ', ' UTF-8 ', $c);
  40. $name. = $c;
  41. }
  42. Else
  43. {
  44. $name. = $str;
  45. }
  46. }
  47. }
  48. return $name;
  49. }
Copy Code
  • 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.