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 .'
';