PHP类--UTF8转GB2312

jackxiang 2010-6-1 21:57 | |

<?php
//UTF8 Convert to gb2312
class Chinese
{
        function Chinese($SourceString)
        {        
                global $Config;
                $this->strings = $SourceString;
                $this->config['tab_dir'] = $Config['rootPath'].'Class/';

                $this->_GetTab();
        }

        function _Bex2bin( $hexdata )
        {
                for ( $i=0; $i<strlen($hexdata); $i+=2 )
                        $bindata.=chr(hexdec(substr($hexdata,$i,2)));

                return $bindata;
        }

        function _GetTab()
        {
                $tmp = @file($this->config['tab_dir'].'utf8-gb2312.tab');
                if (!$tmp)
                        exit("can't open convert tab file!");
                $this->unicode_tab = array();
                while(list($key,$value)=each($tmp))
                        $this->unicode_tab[hexdec(substr($value,7,6))]=substr($value,0,6);
        }

        function UTF8toCHS()
        {
                $out = "";
                $len = strlen($this->strings);
                $i = 0;
                while($i < $len)
                {
                        $c = ord( substr( $this->strings, $i++, 1 ) );
                        switch($c >> 4)
                        {
                                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                                        $out .= substr( $this->strings, $i-1, 1 );
                                break;
                                case 12: case 13:
                                        $char2 = ord( substr( $this->strings, $i++, 1 ) );
                                        $char3 = $this->unicode_tab[(($c & 0x1F) << 6) | ($char2 & 0x3F)];

                                        $out .= $this->_Bex2bin( dechex(  $char3 + 0x8080 ) );
                                break;
                                case 14:
                                        $char2 = ord( substr( $this->strings, $i++, 1 ) );
                                        $char3 = ord( substr( $this->strings, $i++, 1 ) );
                                        $char4 = $this->unicode_tab[(($c & 0x0F) << 12) | (($char2 & 0x3F) << 6) | (($char3 & 0x3F) << 0)];

                                        $out .= $this->_Bex2bin( dechex ( $char4 + 0x8080 ) );

                                break;
                        }
                }
                return $out;
        }
}
?>

来源:http://www.21andy.com/blog/20060817/378.html

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3130/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]