How to convert Unicode encoding to PHP

Source: Internet
Author: User
This article mainly introduced the PHP implementation of Unicode encoding mutual conversion method, combined with examples of the implementation of Unicode encoding and decoding and the use of skills, the need for friends can refer to the following

Specific as follows:

<?php/*** $str The original Chinese String * $encoding the encoding of the original string, the default utf-8* $prefix encoded prefix, the default "" * $postfix the encoded suffix, default ";" */function Unicode_encode ($str, $encoding = ' utf-8 ', $prefix = ', $postfix = '; ')  {//splits the string $str = Iconv ("UTF-8", "gb2312", $str), $cind = 0; $arr _cont = Array (); for ($i = 0; $i < strlen ($STR); $i + +) {if (strlen (substr ($str, $cind, 1)) > 0) {if (Ord (substr ($str, $cind, 1)) < 0XA1) {//1 bytes if in English Array_pus    H ($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 Unicode code foreach ($arr _cont as $ke y = = $value) {$unicodestr. = $prefix. Base_convert (Bin2Hex (iconv (' utf-8 ', ' UCS-4 ', $value)), (+). $postfix;} ret Urn $unicodestr;} /*** $STR Unicode encoded String * $decoding the encoding of the original string, the default utf-8* $prefix the prefix of the encoded string, the default "" * $postfix the suffix of the encoded string, default ";" */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, 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);} $STR = "PHP Programming: www.jb51.net"; $unistr = Unicode_encode ($STR); $unistr 2 = Unicode_decode ($UNISTR); Echo $unistr. ' <br/> '; Echo $unistr 2. ' <br/> '; $unistr = Unicode_encode ($str, ' GBK ', ' \\u '); $unistr 2 = Unicode_decode ($unistr, ' GBK ', ' \\u '); echo $ Unistr. ' <br/> '; Echo $unistr 2. ' <br/> ';

The above is the whole content of this article, I hope that everyone's study has helped.


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.