在《IP地址->地理位置轉換的測評》一文中提到用ip2addr函數直接讀取IP數據庫文件是效率最高的,相比用MySQL數據庫存儲IP數據,用SQL查詢是效率最低的。但是IP數據庫文件QQWry.dat是GB2312編碼的。現在我需要UTF-8編碼的地理位置結果。如果用MySQL方法,可以在數據存入數據庫時就轉換為UTF-8編碼,一勞永逸。但是QQWry.dat文件又無法修改,只能把ip2addr函數的輸出結果再進行動態轉換。
動態轉換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('xxxx', $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 "/r/n"; // 測評文本文件查詢的編碼轉換 $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 "/r/n"; ?> |
測評兩次結果(精確到3位小數,單位是秒):
MySQL查詢轉換:0.112
文本查詢轉換:10.590
MySQL查詢轉換:0.099
文本查詢轉換:10.623
新聞熱點
疑難解答