動態轉換GB->UTF-8編碼至少有四種方法:
用PHP的iconv擴展轉換
用PHP的mb_string擴展轉換
用對換表轉換,對換表存儲在MySQL數據庫中
用對換表轉換,對換表存儲在文本文件中
前兩種方法要服務器作了相應設置(編譯安裝了相應擴展)才能使用。我的虛擬主機沒有這兩個擴展,只好考慮后兩種方法。前兩個方法本文也不進行測評。
測評程序如下(func_ip.php參見《IP地址->地理位置轉換的測評》一文):
<?php
require_once ("func_ip.php");
function u2utf8($c) {
$str = "";
if ($c < 0x80) {
$str .= $c;
} elseif ($c < 0x800) {
$str .= chr(0xC0 | $c >> 6);
$str .= chr(0x80 | $c & 0x3F);
} elseif ($c < 0x10000) {
$str .= chr(0xE0 | $c >> 12);
$str .= chr(0x80 | $c >> 6 & 0x3F);
$str .= chr(0x80 | $c & 0x3F);
} elseif ($c < 0x200000) {
$str .= chr(0xF0 | $c >> 18);
$str .= chr(0x80 | $c >> 12 & 0x3F);
$str .= chr(0x80 | $c >> 6 & 0x3F);
$str .= chr(0x80 | $c & 0x3F);
}
return $str;
}
function GB2UTF8_SQL($strGB) {
if (!trim($strGB)) return $strGB;
$strRet = "";
$intLen = strlen($strGB);
for ($i = 0; $i < $intLen; $i++) {
if (ord($strGB{$i}) > 127) {
$strCurr = substr($strGB, $i, 2);
$intGB = hexdec(bin2hex($strCurr)) - 0x8080;
$strSql = "SELECT code_unicode FROM nnstats_gb_unicode
WHERE code_gb = ".$intGB." LIMIT 1"
;
$resResult = mysql_query($strSql);
if ($arrCode = mysql_fetch_array($resResult)) $strRet .= u2utf8($arrCode["code_unicode"]);
else $strRet .= "??";
$i++;
} else {
$strRet .= $strGB{$i};
}
}
return $strRet;
}
function GB2UTF8_FILE($strGB) {
if (!trim($strGB)) return $strGB;
$arrLines = file("gb_unicode.txt");
foreach ($arrLines as $strLine) {
$arrCodeTable[hexdec(substr($strLine, 0, 6))] = hexdec(substr($strLine, 7, 6));
}
$strRet = "";
$intLen = strlen($strGB);
for ($i = 0; $i < $intLen; $i++) {
if (ord($strGB{$i}) > 127) {
$strCurr = substr($strGB, $i, 2);
$intGB = hexdec(bin2hex($strCurr)) - 0x8080;
if ($arrCodeTable[$intGB]) $strRet .= u2utf8($arrCodeTable[$intGB]);
else $strRet .= "??";
$i++;
} else {
$strRet .= $strGB{$i};
}
}
return $strRet;
}
function EncodeIp($strDotquadIp) {
$arrIpSep = explode('.', $strDotquadIp);
if (count($arrIpSep) != 4) return 0;
$intIp = 0;
foreach ($arrIpSep as $k => $v) $intIp += (int)$v * pow(256, 3 - $k);
return $intIp;
//return sprintf('%02x%02x%02x%02x', $arrIpSep[0], $arrIpSep[1], $arrIpSep[2], $arrIpSep[3]);
}
function GetMicroTime() {
list($msec, $sec) = explode(" ", microtime());
return ((double)$msec + (double)$sec);
}
for ($i = 0; $i < 100; $i++) { // 隨機產生100個ip地址
$strIp = mt_rand(0, 255).".".mt_rand(0, 255).".".mt_rand(0, 255).".".mt_rand(0, 255);
$arrAddr[$i] = ip2addr(EncodeIp($strIp));
}
$resConn = mysql_connect("localhost", "netnest", "netnest");
mysql_select_db("test");
// 測評MySQL查詢的編碼轉換
$dblTimeStart = GetMicroTime();
for ($i = 0; $i < 100; $i++) {
$strUTF8Region = GB2UTF8_SQL($arrAddr[$i]["region"]);
$strUTF8Address = GB2UTF8_SQL($arrAddr[$i]["address"]);
}
$dblTimeDuration = GetMicroTime() - $dblTimeStart;
// 測評結束并輸出結果
echo $dblTimeDuration; echo " ";
// 測評文本文件查詢的編碼轉換
$dblTimeStart = GetMicroTime();
for ($i = 0; $i < 100; $i++) {
$strUTF8Region = GB2UTF8_FILE($arrAddr[$i]["region"]);
$strUTF8Address = GB2UTF8_FILE($arrAddr[$i]["address"]);
}
$dblTimeDuration = GetMicroTime() - $dblTimeStart;
// 測評結束并輸出結果
echo $dblTimeDuration; echo " ";
?>
測評兩次結果(精確到3位小數,單位是秒):
MySQL查詢轉換:0.112
文本查詢轉換:10.590
MySQL查詢轉換:0.099
文本查詢轉換:10.623
可見這次是MySQL方法遙遙領先于文件查詢法。但是現在還不急于使用MySQL方法,因為文本文件方法之所以如此耗時,主要因為它每次轉換都要把整個gb_unicode.txt讀入內存,而gb_unicode.txt又是文本文件,格式如下:
0x2121 0x3000 # IDEOGRAPHIC SPACE
0x2122 0x3001 # IDEOGRAPHIC COMMA
0x2123 0x3002 # IDEOGRAPHIC FULL STOP
0x2124 0x30FB # KATAKANA MIDDLE DOT
0x2125 0x02C9 # MODIFIER LETTER MACRON (Mandarin Chinese first tone)
……
0x552A 0x6458 # <CJK>
0x552B 0x658B # <CJK>
0x552C 0x5B85 # <CJK>
0x552D 0x7A84 # <CJK>
……
0x777B 0x9F37 # <CJK>
0x777C 0x9F3D # <CJK>
0x777D 0x9F3E # <CJK>
0x777E 0x9F44 # <CJK>
文本文件效率較低,于是考慮把文本文件轉換為二進制文件,然后用折半法查找這個文件,而不需要把整個文件讀入內存。文件格式為:文件頭2字節,存儲記錄數;接著一條接一條記錄存入文件,每條記錄4字節,前2字節對應GB代碼,后2字節對應Unicode代碼。轉換程序如下:
<?php
$arrLines = file("gb_unicode.txt");
foreach ($arrLines as $strLine) {
$arrCodeTable[hexdec(substr($strLine, 0, 6))] = hexdec(substr($strLine, 7, 6));
}
ksort($arrCodeTable);
$intCount = count($arrCodeTable);
$strCount = chr($intCount % 256) . chr(floor($intCount / 256));
$fileGBU = fopen("gbu.dat", "wb");
fwrite($fileGBU, $strCount);
foreach ($arrCodeTable as $k => $v) {
$strData = chr($k % 256) . chr(floor($k / 256)) . chr($v % 256) . chr(floor($v / 256));
fwrite($fileGBU, $strData);
}
fclose($fileGBU);
?>
執行程序后就獲得了二進制的GB->Unicode對照表gbu.dat,并且數據記錄按GB代碼排了序,便于折半法查找。使用gbu.dat進行轉碼的函數如下:
function GB2UTF8_FILE1($strGB) {
if (!trim($strGB)) return $strGB;
$fileGBU = fopen("gbu.dat", "rb");
$strBuf = fread($fileGBU, 2);
$intCount = ord($strBuf{0}) + 256 * ord($strBuf{1});
$strRet = "";
$intLen = strlen($strGB);
for ($i = 0; $i < $intLen; $i++) {
if (ord($strGB{$i}) > 127) {
$strCurr = substr($strGB, $i, 2);
$intGB = hexdec(bin2hex($strCurr)) - 0x8080;
$intStart = 1;
$intEnd = $intCount;
while ($intStart < $intEnd - 1) { // 折半法查找
$intMid = floor(($intStart + $intEnd) / 2);
$intOffset = 2 + 4 * ($intMid - 1);
fseek($fileGBU, $intOffset);
$strBuf = fread($fileGBU, 2);
$intCode = ord($strBuf{0}) + 256 * ord($strBuf{1});
if ($intGB == $intCode) {
$intStart = $intMid;
break;
}
if ($intGB > $intCode) $intStart = $intMid;
else $intEnd = $intMid;
}
$intOffset = 2 + 4 * ($intStart - 1);
fseek($fileGBU, $intOffset);
$strBuf = fread($fileGBU, 2);
$intCode = ord($strBuf{0}) + 256 * ord($strBuf{1});
if ($intGB == $intCode) {
$strBuf = fread($fileGBU, 2);
$intCodeU = ord($strBuf{0}) + 256 * ord($strBuf{1});
$strRet .= u2utf8($intCodeU);
} else {
$strRet .= "??";
}
$i++;
} else {
$strRet .= $strGB{$i};
}
}
return $strRet;
}
把其加到原來的測評程序,對三種方法同時測評2次得到數據(精確到3位小數,單位:秒):
MySQL方法:0.125
文本文件方法:10.873
二進制文件折半法:0.106
MySQL方法:0.102
文本文件方法:10.677
二進制文件折半法:0.092
可見二進制文件折半法還比MySQL法略有優勢。但是上述測評都是對短的地理位置進行轉碼,如果對較長的文本轉碼又如何呢?我找來5個Blog的RSS 2.0文件,都是GB2312編碼。測評三種方法對5個文件編碼耗費的時間,2次測量數據如下(精確到3位小數,單位:秒):
MySQL方法:7.206
文本文件方法:0.772
二進制文件折半法:5.022
MySQL方法:7.440
文本文件方法:0.766
二進制文件折半法:5.055
可見對長的文本是用文本文件的方法最優,因為轉碼對照表讀入內存后,轉碼就可以很高效了。既然如此,我們還可以嘗試改進一下,把文本文件方法改為:轉碼對照表從二進制文件gbu.dat讀入內存,而不是文本文件。測評數據如下(精度和單位同上):
從文本文件讀入對照表:0.766
從二進制文件讀入對照表:0.831
從文本文件讀入對照表:0.774
從二進制文件讀入對照表:0.833
表明這次改進失敗了,從文本文件讀入轉碼對照表更高效。
總結:用PHP對GB編碼到UTF-8編碼的動態轉換,如果每次轉換的文本很小,適宜用二進制文件結合折半法轉換;如果每次轉換的文本較大,適宜用文本文件存儲轉碼對照表,并在轉換前一次性把對照表讀入內存。
深圳北大青鳥嘉華校區http://www.sbsnmc.com