Example of mutual conversion of PHPUnicode encoding-PHP source code

Source: Internet
Author: User
Unicode encoding is not used much because Unicode encoding is a character in the source code, but sometimes Unicode encoding is used. Let's take a look at an example of mutual conversion of PHPUnicode encoding, I hope the example will be helpful to you. Unicode encoding is not used much because Unicode encoding is a character in the source code, but sometimes Unicode encoding is used. Let's take a look at an example of mutual conversion between PHP Unicode encoding, I hope the example will be helpful to you.

Script ec (2); script

 0) {if (ord (substr ($ str, $ cind, 1) <0xA1) {// if it is English, 1 byte array_push ($ arr_cont, substr ($ str, $ cind, 1); $ cind ++;} else {array_push ($ arr_cont, substr ($ str, $ cind, 2 )); $ cind + = 2 ;}} foreach ($ arr_cont as & $ row) {$ row = iconv ("gb2312", "UTF-8", $ row );} // convert the Unicode code foreach ($ arr_cont as $ key => $ value) {$ unicodestr. = $ prefix. base_convert (bin2hex (iconv ('utf-8', 'ucs-4', $ value), 16, 10). $ Postfix;} return $ unicodestr;}/*** $ str Unicode encoded string * $ encoding of the original decoding string. The default value is UTF-8 * $ prefix, the default value is "& #" * $ postfix: The suffix of the encoded string. The default value is ";" */function unicode_decode ($ unistr, $ encoding = 'utf-8 ', $ 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, strl En ($ arruni [$ I])-strlen ($ postfix) ;}$ temp = intval ($ arruni [$ I]); $ unistr. = ($ temp & lt; 256 )? Chr (0 ). chr ($ temp): chr ($ temp/256 ). chr ($ temp % 256);} return iconv ('ucs-2', $ encoding, $ unistr);} $ str = "PHP secondary development: www. php2.cc "; $ unistr = unicode_encode ($ str); $ unistr2 = unicode_decode ($ unistr); echo $ unistr.'
'; Echo $ unistr2 .'
'; $ Unistr = unicode_encode ($ str, 'gbk',' \ U'); $ unistr2 = unicode_decode ($ unistr, 'gbk', '\ U '); echo $ unistr.'
'; Echo $ unistr2 .'
';

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.