Php Chinese character transcoding Unicode encoding function
- /**
- * $ 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;
- } // Script School bbs.it-home.org
-
- /**
- * $ 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 of the php Chinese character conversion function to implement encoding conversion:
- // 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 .'
'; // \ U60 \ u98 \ u62 \ u21704 \ u21704 \ u60 \ u47 \ u98 \ u62
-
- $ Profix_unistr2 = unicode_decode ($ prefix_unistr, 'gbk', "\ u ",'');
- Echo $ profix_unistr2 .'
';//Haha
|