/** * $str the original String * $encoding the encoding of the original string, the default GBK * $prefix the encoded prefix, the default "*" * $postfix the encoded suffix, the default ";" * *functionUnicode_encode($str, $encoding = ' GBK ', $prefix = ' the ', $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 the encoding of the original string, the default GBK * $prefix encoded string prefix, the default "* * * * $postfix the suffix of the encoded string, default"; "* *functionUnicode_decode($unistr, $encoding = ' GBK ', $prefix = ' the ', $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 < the)?CHR(0) . CHR($temp) : CHR($temp / the) . CHR($temp % the); } return Iconv(' UCS-2 ', $encoding, $unistr);}
Use:
//GBK String Test$str = ' <b> haha </b> ';Echo $str.' <br/> ';?$unistr =Unicode_encode($str);Echo $unistr.' <br/> '; //& #60;& #98;& #62;& #21704;& #21704;& #60;& #47;& #98;& #62;?$str 2 =Unicode_decode($unistr);Echo $str 2.' <br/> '; //<b> haha </b>?//utf-8 String Test$utf 8_str = Iconv(' GBK ', ' UTF-8 ', $str);Echo $utf 8_str.' <br/> '; //<b> å 堝 搱 </b> Note: UTF shows the garbled characters in GBK! Switchable browser-coded test?$utf 8_unistr =Unicode_encode($utf 8_str, ' UTF-8 ');Echo $utf 8_unistr.' <br/> '; //& #60;& #98;& #62;& #21704;& #21704;& #60;& #47;& #98;& #62;?$utf 8_str2 =Unicode_decode($utf 8_unistr, ' UTF-8 ');Echo $utf 8_str2.' <br/> '; //<b> å 堝 搱 </b>?//Other suffix, prefix test$prefix _unistr =Unicode_encode($str, ' GBK ', "\ \U", "');Echo $prefix _unistr.' <br/> '; //\u60\u98\u62\u21704\u21704\u60\u47\u98\u62?$profix _unistr2 =Unicode_decode($prefix _unistr, ' GBK ', "\ \U", "');Echo $profix _unistr2.' <br/> '; //<b> haha </b>