php Chinese unicode Huzhuan

/**
 * $ Str original string Chinese
 * Coding $ encoding the original string, the default GBK
 * $ Prefix after the prefix code, the default "& #"
 * Suffix after $ postfix code, default ";"
 */
function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $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;
}
  
/**
 * After a string $ str Unicode encoding
 * $ Decoding coded the original string, the default GBK
 * $ Prefix prefix encoded string, the default "& #"
 * $ Postfix suffix encoded string, the default ";"
 */
function unicode_decode($unistr, $encoding = 'GBK', $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);
}
 
// GBK test string 
$ STR = '<B> ha </ B>' ;
 echo  $ STR . '<br />' ;
  
$unistr = unicode_encode($str);
echo $unistr.'<br />'; // <b>哈哈</b>
  
$str2 = unicode_decode($unistr);
echo $str2.'<br />'; //<b>哈哈</b>
  
// UTF-8 strings test 
$ utf8_str = iconv ( 'GBK', 'UTF-. 8', $ STR );
 echo  $ utf8_str . '<br />'; // <B> crucible Zhi Yu </ b> Note: UTF garbled display at GBK! Switchable test code browser
  
$utf8_unistr = unicode_encode($utf8_str, 'UTF-8');
echo $utf8_unistr.'<br />'; // <b>哈哈</b>
  
$utf8_str2 = unicode_decode($utf8_unistr, 'UTF-8');
echo $utf8_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>哈哈</b>

 

Guess you like

Origin www.cnblogs.com/binblogs/p/12060856.html