Share PHP code UTF-8 and Unicode code interchange (multilingual)

Source: Internet
Author: User
: This article mainly introduces the sharing of PHP code UTF-8 and Unicode code conversion (multi-language), for PHP tutorials interested in students can refer. PHP UTF-8 and Unicode code conversion
/*** // Encode the content in UNICODE format * UTF-8 to unicode ** @ param string $ name * @ return string */function utf8_unicode ($ name) {$ name = iconv ('utf-8', 'ucs-2', $ name); $ len = strlen ($ name); $ str = ''; for ($ I = 0; $ I <$ len-1; $ I = $ I + 2) {$ c = $ name [$ I]; $ c2 = $ name [$ I + 1]; if (ord ($ c)> 0) {// two bytes of text $ str. = '\ U '. base_convert (ord ($ c), 10, 16 ). str_pad (base_convert (ord ($ c2), 10, 16), 2, 0, STR_PAD_LEFT); // $ Str. = base_convert (ord ($ c), 10, 16 ). str_pad (base_convert (ord ($ c2), 10, 16), 2, 0, STR_PAD_LEFT);} else {$ str. = '\ U '. str_pad (base_convert (ord ($ c2), 10, 16), 4, 0, STR_PAD_LEFT); // $ str. = str_pad (base_convert (ord ($ c2), 10, 16), 4, 0, STR_PAD_LEFT) ;}}$ str = strtoupper ($ str ); // Convert to uppercase return $ str;}/*** unicode to UTF-8 ** @ param string $ name * @ return string */function unicode_decodessss ($ na Me) {$ name = strtolower ($ name); // Convert the Unicode encoding to the UTF-8 encoding that can be viewed $ pattern = '/([\ w] +) | (\ u ([\ w] {4})/I '; preg_match_all ($ pattern, $ name, $ matches); if (! Empty ($ matches) {$ name = ''; for ($ j = 0; $ j <count ($ matches [0]); $ j ++) {$ str = $ matches [0] [$ j]; if (strpos ($ str, '\ u') = 0) {$ code = base_convert (substr ($ str, 2, 2), 16, 10); $ code2 = base_convert (substr ($ str, 4), 16, 10 ); $ c = chr ($ code ). chr ($ code2); $ c = iconv ('ucs-2', 'utf-8', $ c); $ name. = $ c;} else {$ name. = $ str ;}}return $ name ;}

Call and result:
$ Utf8_str = 'I'; // This is the Unicode code of the Chinese character "you" $ unicode_str = '\ u4f60'; // The output 6211 echo utf8_unicode ($ utf8_str )."
"; // Output the Chinese character" you "echo unicode_decodes ($ unicode_str );

Note: Because the browser will interpret it by default, you should check the source code.
\ U6211
You

The above introduces the sharing of PHP code UTF-8 and Unicode code conversion (multi-language), including the content of the aspect, hope to be interested in PHP Tutorial friends help.

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.