Php Chinese character transcoding Unicode encoding function

Source: Internet
Author: User
Php Chinese character transcoding Unicode encoding function

  1. /**
  2. * $ Str original string
  3. * $ Encoding of the original encoding string. the default value is GBK.
  4. * $ Prefix: the prefix after Encoding. the default value is "&#"
  5. * $ Postfix: the suffix after Encoding. the default value is ";"
  6. */
  7. Function unicode_encode ($ str, $ encoding = 'gbk', $ prefix = '& #', $ postfix = ';'){
  8. $ Str = iconv ($ encoding, 'ucs-2', $ str );
  9. $ Arrstr = str_split ($ str, 2 );
  10. $ Unistr = '';
  11. For ($ I = 0, $ len = count ($ arrstr); $ I <$ len; $ I ++ ){
  12. $ Dec = hexdec (bin2hex ($ arrstr [$ I]);
  13. $ Unistr. = $ prefix. $ dec. $ postfix;
  14. }
  15. Return $ unistr;
  16. } // Script School bbs.it-home.org
  17. /**
  18. * $ Str Unicode encoded string
  19. * $ Encoding of the original encoding string. the default value is GBK.
  20. * $ Prefix: prefix of the encoded string. the default value is "&#"
  21. * $ Postfix: specifies the suffix of the encoded string. the default value is ";"
  22. */
  23. Function unicode_decode ($ unistr, $ encoding = 'gbk', $ prefix = '& #', $ postfix = ';'){
  24. $ Arruni = explode ($ prefix, $ unistr );
  25. $ Unistr = '';
  26. For ($ I = 1, $ len = count ($ arruni); $ I <$ len; $ I ++ ){
  27. If (strlen ($ postfix)> 0 ){
  28. $ Arruni [$ I] = substr ($ arruni [$ I], 0, strlen ($ arruni [$ I])-strlen ($ postfix ));
  29. }
  30. $ Temp = intval ($ arruni [$ I]);
  31. $ Unistr. = ($ temp <256 )? Chr (0). chr ($ temp): chr ($ temp/256). chr ($ temp % 256 );
  32. }
  33. Return iconv ('ucs-2', $ encoding, $ unistr );
  34. }

Example of the php Chinese character conversion function to implement encoding conversion:

  1. // GBK string test
  2. $ Str ='Haha';
  3. Echo $ str .'
    ';
  4. $ Unistr = unicode_encode ($ str );
  5. Echo $ unistr .'
    '; // <B> Haha </B>
  6. $ Str2 = unicode_decode ($ unistr );
  7. Echo $ str2 .'
    ';//Haha
  8. // UTF-8 string test
  9. $ Utf8_str = iconv ('gbk', 'utf-8', $ str );
  10. Echo $ utf8_str .'
    ';//Note: garbled characters of UTF in GBK are displayed! You can switch the encoding test of the browser.
  11. $ Utf8_unistr = unicode_encode ($ utf8_str, 'utf-8 ');
  12. Echo $ utf8_unistr .'
    '; // <B> Haha </B>
  13. $ Utf8_str2 = unicode_decode ($ utf8_unistr, 'utf-8 ');
  14. Echo $ utf8_str2 .'
    ';//
  15. // Test other suffixes and prefixes
  16. $ Prefix_unistr = unicode_encode ($ str, 'gbk', "\ u ",'');
  17. Echo $ prefix_unistr .'
    '; // \ U60 \ u98 \ u62 \ u21704 \ u21704 \ u60 \ u47 \ u98 \ u62
  18. $ Profix_unistr2 = unicode_decode ($ prefix_unistr, 'gbk', "\ u ",'');
  19. Echo $ profix_unistr2 .'
    ';//Haha

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.