Php ing Chinese characters to Unicode encoding _ PHP Tutorial

Source: Internet
Author: User
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...

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.