| This article introduces two examples of unicode encoding and decoding in php. For more information, see. Example 1: The functions of php are preg_match_all and empty.
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);} else {$ str. = $ c2 ;}return $ str ;}// decodes the UNICODE-encoded content. function unicode_decode ($ name) {// converts the encoding, convert Unicode encoding to 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 ;}?>Example 2: The php function json_decode is used in this example.
|