Php anti-Chinese characters to Unicode encoding implementation program. This article introduces the php program for converting Chinese characters to Unicode encoding. For more information, see. The program code is as follows: Copy the code *** $ str original string this article will introduce you to php anti-Chinese characters to Unicode encoding implementation methods. if you need to know, go to the reference.
Program
The code is as follows: |
|
/** * $ Str original string * $ Encoding of the original encoding string. the default value is GBK. * $ Prefix: the prefix after Encoding. the default value is "&#" * $ Postfix: the suffix after Encoding. the default value is ";" */ Function unicode_encode ($ str, $ encoding = 'gbk', $ prefix = '& #', $ postfix = ';'){ $ Str = iconv ($ encoding, 'ucs-2', $ str ); $ Arrstr = str_split ($ str, 2 ); $ Unistr = ''; For ($ I = 0, $ len = count ($ arrstr); $ I <$ len; $ I ++ ){ $ Dec = hexdec (bin2hex ($ arrstr [$ I]); $ Unistr. = $ prefix. $ dec. $ postfix; } Return $ unistr; } /** * $ Str Unicode encoded string * $ Encoding of the original encoding string. the default value is GBK. * $ Prefix: prefix of the encoded string. the default value is "&#" * $ Postfix: specifies the suffix of the encoded string. the default value is ";" */ Function unicode_decode ($ unistr, $ encoding = 'gbk', $ prefix = '& #', $ postfix = ';'){ $ Arruni = explode ($ prefix, $ unistr ); $ Unistr = ''; For ($ I = 1, $ len = count ($ arruni); $ I <$ len; $ I ++ ){ If (strlen ($ postfix)> 0 ){ $ Arruni [$ I] = substr ($ arruni [$ I], 0, strlen ($ arruni [$ I])-strlen ($ postfix )); } $ Temp = intval ($ arruni [$ I]); $ Unistr. = ($ temp <256 )? Chr (0). chr ($ temp): chr ($ temp/256). chr ($ temp % 256 ); } Return iconv ('ucs-2', $ encoding, $ unistr ); } |
Example:
The code is as follows: |
|
// GBK string test $ Str ='Haha'; Echo $ str .' '; $ Unistr = unicode_encode ($ str ); Echo $ unistr .' '; // <B> Haha </B> $ Str2 = unicode_decode ($ unistr ); Echo $ str2 .' ';//Haha // UTF-8 string test $ Utf8_str = iconv ('gbk', 'utf-8', $ str ); Echo $ utf8_str .' ';//????Note: garbled characters of UTF in GBK are displayed! You can switch the encoding test of the browser. $ Utf8_unistr = unicode_encode ($ utf8_str, 'utf-8 '); Echo $ utf8_unistr .' '; // <B> Haha </B> $ Utf8_str2 = unicode_decode ($ utf8_unistr, 'utf-8 '); Echo $ utf8_str2 .' ';//???? // Test other suffixes and prefixes $ Prefix_unistr = unicode_encode ($ str, 'gbk', "\ u ",''); Echo $ prefix_unistr .' '; // U60u98u62u21704u21704u60u47u98u62 $ Profix_unistr2 = unicode_decode ($ prefix_unistr, 'gbk', "\ u ",''); Echo $ profix_unistr2 .' ';//Haha |
Bytes. The program code is as follows/*** $ str original string...