php实现繁简转换

 

class Big5_GB2312_Transfer{
    private $utf8_gb2312;
    private $utf8_big5;
    
    public function __construct(){
        $this->utf8_gb2312 = file_get_contents('./gb2312.map');
        $this->utf8_big5 = file_get_contents('./big5.map');
    }
    
    public function c2t($str) {
        $str_t = '';
        $len = strlen($str);
        $a = 0;
        while ($a < $len){
            if (ord($str{$a})>=224 && ord($str{$a})<=239){
                if (($temp = strpos( $this->utf8_gb2312, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
                    $str_t .= $this->utf8_big5{$temp} . $this->utf8_big5{$temp+1} . $this->utf8_big5{$temp+2};
                    $a += 3;
                    continue;
                }
            }
            $str_t .= $str{$a};
            $a += 1;
        }
        return $str_t;
    }
    
    public function t2c($str) {
        $str_t = '';
        $len = strlen($str);
        $a = 0;
        while ($a < $len){
            if (ord($str{$a})>=224 && ord($str{$a})<=239){
                if (($temp = strpos( $this->utf8_big5, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
                    $str_t .= $this->utf8_gb2312{$temp} . $this->utf8_gb2312{$temp+1} . $this->utf8_gb2312{$temp+2};
                    $a += 3;
                    continue;    
                }
            }
            $str_t .= $str{$a};
            $a += 1;
        }
        return $str_t;
    }
}

big5_gb2312_map.zip

posted on 2012-08-02 16:15  ianarfa  阅读(400)  评论(0编辑  收藏  举报

导航