: 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.