将unicode编码的中文汉字转变成UTF-8编码的php程序

<?php
function unescape($source, $iconv_to = 'UTF-8') {
    $decodedStr = '';
    $pos = 0;
    $len = strlen($source);
    while ( $pos < $len ) {
        $charAt = substr($source , $pos , 1);
        if($charAt == '//') {
            $pos++;
            $charAt = substr($source , $pos , 1);
            if($charAt == 'u') {
                $pos++;
                $unicodeHexVal = substr($source , $pos , 4);
                $unicode = hexdec($unicodeHexVal);
                $decodedStr .= code2utf($unicode);
                $pos += 4;
            } else {
                $hexVal = substr($source , $pos , 2);
                $decodedStr .= chr(hexdec($hexVal));
                $pos += 2;
            }
        } else {
            $decodedStr .= $charAt;
            $pos++;
        }
    }
    if($iconv_to != "UTF-8") {
        $decodedStr = iconv("UTF-8" , $iconv_to , $decodedStr);
    }
    
    return $decodedStr;
}
function code2utf($num) {
    if($num < 128) return chr($num);
    if($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
    if($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
    if($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
    return '';
}

$file_name = "piao.sort";
$file = file($file_name);
$data_array = array();
foreach($file as $line)
{
    //ECHO (DEBUG_LOG,"TRACE : line to array /n",__FILE__,__FUNCTION__,__LINE__);
    if(strlen($line) != 0)
    {
        $data = unescape($line);
        
        //ECHO (DEBUG_LOG,"TRACE : array poi_id  ".$data[0]." /n",__FILE__,__FUNCTION__,__LINE__);
        //ECHO (DEBUG_LOG,"TRACE : array expenditure  ".$data[4]." /n",__FILE__,__FUNCTION__,__LINE__);
        
        echo mb_convert_encoding($data,"GBK","UTF-8");
    }
    else
    {
        //ECHO (WARN_LOG,'WARN : line is null',__FILE__,__FUNCTION__,__LINE__);
    }
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值