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.