天天看點

DedeCMS中用到的字元編碼轉換1

php自帶的iconv和mbstring庫都可以完成這項工作,但一般的虛拟主機很少支援

其中的全局變量在include/common.inc.php

庫檔案在include/data下

DedeCMS中用到的字元編碼轉換1

<?php  if(!defined('dedeinc')) exit('dedecms');  

/** 

 * dedecms中用到的字元編碼轉換的小助手函數 

 * 

 * @version        $id: charset.helper.php 1 2010-07-05 11:43:09z tianya $ 

 * @package        dedecms.helpers 

 * @copyright      copyright (c) 2007 - 2010, desdev, inc. 

 * @license        http://help.dedecms.com/usersguide/license.html 

 * @link           http://www.dedecms.com 

 */  

$uc2gbtable = $codetable = $big5_data = $gb_data = '';  

$gbkunidic = null;  

 *  utf-8 轉gb編碼 

 * @access    public 

 * @param     string  $utfstr  需要轉換的字元串 

 * @return    string 

if ( ! function_exists('utf82gb'))  

{  

    function utf82gb($utfstr)  

    {  

        if(function_exists('iconv'))  

        {  

            return iconv('utf-8','gbk//ignore',$utfstr);  

        }  

        global $uc2gbtable;  

        $okstr = "";  

        if(trim($utfstr)=="")  

            return $utfstr;  

        if(empty($uc2gbtable))  

            $filename = dedeinc."/data/gb2312-utf8.dat";  

            $fp = fopen($filename,"r");  

            while($l = fgets($fp,15))  

            {  

                $uc2gbtable[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));  

            }  

            fclose($fp);  

        $ulen = strlen($utfstr);  

        for($i=0;$i<$ulen;$i++)  

            $c = $utfstr[$i];  

            $cb = decbin(ord($utfstr[$i]));  

            if(strlen($cb)==8)  

                $csize = strpos(decbin(ord($cb)),"0");  

                for($j=0;$j < $csize;$j++)  

                {  

                    $i++; $c .= $utfstr[$i];  

                }  

                $c = utf82u($c);  

                if(isset($uc2gbtable[$c]))  

                    $c = dechex($uc2gbtable[$c]+0x8080);  

                    $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));  

                else  

                    $okstr .= "&#".$c.";";  

            else  

                $okstr .= $c;  

        $okstr = trim($okstr);  

        return $okstr;  

    }  

}  

 *  gb轉utf-8編碼 

 * @param     string  $gbstr  gbk的字元串 

if ( ! function_exists('gb2utf8'))  

    function gb2utf8($gbstr)  

            return iconv('gbk','utf-8//ignore',$gbstr);  

        global $codetable;  

        if(trim($gbstr)=="")  

            return $gbstr;  

        if(empty($codetable))  

            while ($l = fgets($fp,15))  

                $codetable[hexdec(substr($l, 0, 6))] = substr($l, 7, 6);  

        $ret = "";  

        $utf8 = "";  

        while ($gbstr != '')  

            if (ord(substr($gbstr, 0, 1)) > 0x80)  

                $thisw = substr($gbstr, 0, 2);  

                $gbstr = substr($gbstr, 2, strlen($gbstr));  

                $utf8 = "";  

                @$utf8 = u2utf8(hexdec($codetable[hexdec(bin2hex($thisw)) - 0x8080]));  

                if($utf8!="")  

                    for ($i = 0;$i < strlen($utf8);$i += 3)  

                    $ret .= chr(substr($utf8, $i, 3));  

                $ret .= substr($gbstr, 0, 1);  

                $gbstr = substr($gbstr, 1, strlen($gbstr));  

        return $ret;  

 *  unicode轉utf8 

 * @param     string  $c  unicode的字元串内容 

if ( ! function_exists('u2utf8'))  

    function u2utf8($c)  

        for ($i = 0;$i < count($c);$i++)  

            $str = "";  

        if ($c < 0x80)  

            $str .= $c;  

        else if ($c < 0x800)  

            $str .= (0xc0 | $c >> 6);  

            $str .= (0x80 | $c & 0x3f);  

        else if ($c < 0x10000)  

            $str .= (0xe0 | $c >> 12);  

            $str .= (0x80 | $c >> 6 & 0x3f);  

        else if ($c < 0x200000)  

            $str .= (0xf0 | $c >> 18);  

            $str .= (0x80 | $c >> 12 & 0x3f);  

        return $str;  

 *  utf8轉unicode 

 * @param     string  $c  utf-8的字元串資訊 

if ( ! function_exists('utf82u'))  

    function utf82u($c)  

        switch(strlen($c))  

            case 1:  

                return ord($c);  

            case 2:  

                $n = (ord($c[0]) & 0x3f) << 6;  

                $n += ord($c[1]) & 0x3f;  

                return $n;  

            case 3:  

                $n = (ord($c[0]) & 0x1f) << 12;  

                $n += (ord($c[1]) & 0x3f) << 6;  

                $n += ord($c[2]) & 0x3f;  

            case 4:  

                $n = (ord($c[0]) & 0x0f) << 18;  

                $n += (ord($c[1]) & 0x3f) << 12;  

                $n += (ord($c[2]) & 0x3f) << 6;  

                $n += ord($c[3]) & 0x3f;  

 *  big5碼轉換成gb碼 

 * @param     string   $text  字元串内容 

if ( ! function_exists('big52gb'))  

    function big52gb($text)  

            return iconv('big5','gbk//ignore',$text);  

        global $big5_data;  

        if(empty($big5_data))  

            $filename = dedeinc."/data/big5-gb.dat";  

            $fp = fopen($filename, "rb");  

            $big5_data = fread($fp,filesize($filename));  

        $max = strlen($text)-1;  

        for($i=0;$i<$max;$i++)  

            $h = ord($text[$i]);  

            if($h>=0x80)  

                $l = ord($text[$i+1]);  

                if($h==161 && $l==64)  

                    $gbstr = " ";  

                    $p = ($h-160)*510+($l-1)*2;  

                    $gbstr = $big5_data[$p].$big5_data[$p+1];  

                $text[$i] = $gbstr[0];  

                $text[$i+1] = $gbstr[1];  

                $i++;  

        return $text;  

 *  gb碼轉換成big5碼 

 * @param     string  $text 字元串内容 

if ( ! function_exists('gb2big5'))  

    function gb2big5($text)  

            return iconv('gbk','big5//ignore',$text);  

        global $gb_data;  

        if(empty($gb_data))  

            $filename = dedeinc."/data/gb-big5.dat";  

            $gb = fread($fp,filesize($filename));  

                    $big = " ";  

                    $big = $gb_data[$p].$gb_data[$p+1];  

                $text[$i] = $big[0];  

                $text[$i+1] = $big[1];  

 *  unicode url編碼轉gbk編碼函數 

 * @param     string  $str  轉換的内容 

if ( ! function_exists('unicodeurl2gbk'))  

    function unicodeurl2gbk($str)  

        //載入對照詞典  

        if(!isset($globals['gbkunidic']))  

            $fp = fopen(dedeinc.'/data/gbk-unicode.dat','rb');  

            while(!feof($fp))  

                $globals['gbkunidic'][bin2hex(fread($fp,2))] = fread($fp,2);  

        //處理字元串  

        $str = str_replace('$#$','+',$str);  

        $glen = strlen($str);  

        for($i=0; $i < $glen; $i++)  

            if($glen-$i > 4)  

                if($str[$i]=='%' && $str[$i+1]=='u')  

                    $uni = strtolower(substr($str,$i+2,4));  

                    $i = $i+5;  

                    if(isset($globals['gbkunidic'][$uni]))  

                    {  

                        $okstr .= $globals['gbkunidic'][$uni];  

                    }  

                    else  

                        $okstr .= "&#".hexdec('0x'.$uni).";";  

                    $okstr .= $str[$i];  

                $okstr .= $str[$i];  

 *  自動轉換字元集 支援數組轉換 

if ( ! function_exists('autocharset'))  

    function autocharset($fcontents, $from='gbk', $to='utf-8')  

        $from   =  strtoupper($from)=='utf8'? 'utf-8' : $from;  

        $to       =  strtoupper($to)=='utf8'? 'utf-8' : $to;  

        if( strtoupper($from) === strtoupper($to) || empty($fcontents) || (is_scalar($fcontents) && !is_string($fcontents)) ){  

            //如果編碼相同或者非字元串标量則不轉換  

            return $fcontents;  

        if(is_string($fcontents) )   

            if(function_exists('mb_convert_encoding'))  

                return mb_convert_encoding ($fcontents, $to, $from);  

            } elseif (function_exists('iconv'))  

                return iconv($from, $to, $fcontents);  

            } else {  

                return $fcontents;  

        elseif(is_array($fcontents))  

            foreach ( $fcontents as $key => $val )   

                $_key =     autocharset($key,$from,$to);  

                $fcontents[$_key] = autocharset($val,$from,$to);  

                if($key != $_key )  

                    unset($fcontents[$key]);  

        else{  

?>