php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)_PHP教程
推薦:php配置文件php.ini的中文注釋版這個(gè)文件控制了PHP許多方面的觀點(diǎn)。為了讓PHP讀取這個(gè)文件,它必須被命名為 ; ´php.ini´。PHP 將在這些地方依次查找該文件:當(dāng)前工作目錄;環(huán)境變量PHPRC ; 指明的路徑;
在《IP地址->地理位置轉(zhuǎn)換的測(cè)評(píng)》一文中提到用ip2addr函數(shù)直接讀取IP數(shù)據(jù)庫文件是效率最高的,相比用MySQL數(shù)據(jù)庫存儲(chǔ)IP數(shù)據(jù),用SQL查詢是效率最低的。但是IP數(shù)據(jù)庫文件QQWry.dat是GB2312編碼的。現(xiàn)在我需要UTF-8編碼的地理位置結(jié)果。如果用MySQL方法,可以在數(shù)據(jù)存入數(shù)據(jù)庫時(shí)就轉(zhuǎn)換為UTF-8編碼,一勞永逸。但是QQWry.dat文件又無法修改,只能把ip2addr函數(shù)的輸出結(jié)果再進(jìn)行動(dòng)態(tài)轉(zhuǎn)換。
動(dòng)態(tài)轉(zhuǎn)換GB->UTF-8編碼至少有四種方法:
用PHP的iconv擴(kuò)展轉(zhuǎn)換
用PHP的mb_string擴(kuò)展轉(zhuǎn)換
用對(duì)換表轉(zhuǎn)換,對(duì)換表存儲(chǔ)在MySQL數(shù)據(jù)庫中
用對(duì)換表轉(zhuǎn)換,對(duì)換表存儲(chǔ)在文本文件中
前兩種方法要服務(wù)器作了相應(yīng)設(shè)置(編譯安裝了相應(yīng)擴(kuò)展)才能使用。我的虛擬主機(jī)沒有這兩個(gè)擴(kuò)展,只好考慮后兩種方法。前兩個(gè)方法本文也不進(jìn)行測(cè)評(píng)。
測(cè)評(píng)程序如下(func_ip.php參見《IP地址->地理位置轉(zhuǎn)換的測(cè)評(píng)》一文):
<?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 ) { // 隨機(jī)產(chǎn)生100個(gè)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"); // 測(cè)評(píng)MySQL查詢的編碼轉(zhuǎn)換 $dblTimeStart = GetMicroTime(); for ($i = 0; $i < 100; $i ) { $strUTF8Region = GB2UTF8_SQL($arrAddr[$i]["region"]); $strUTF8Address = GB2UTF8_SQL($arrAddr[$i]["address"]); } $dblTimeDuration = GetMicroTime() - $dblTimeStart; // 測(cè)評(píng)結(jié)束并輸出結(jié)果 echo $dblTimeDuration; echo "\r\n"; // 測(cè)評(píng)文本文件查詢的編碼轉(zhuǎn)換 $dblTimeStart = GetMicroTime(); for ($i = 0; $i < 100; $i ) { $strUTF8Region = GB2UTF8_FILE($arrAddr[$i]["region"]); $strUTF8Address = GB2UTF8_FILE($arrAddr[$i]["address"]); } $dblTimeDuration = GetMicroTime() - $dblTimeStart; // 測(cè)評(píng)結(jié)束并輸出結(jié)果 echo $dblTimeDuration; echo "\r\n"; ?> |
測(cè)評(píng)兩次結(jié)果(精確到3位小數(shù),單位是秒):
MySQL查詢轉(zhuǎn)換:0.112
文本查詢轉(zhuǎn)換:10.590
MySQL查詢轉(zhuǎn)換:0.099
文本查詢轉(zhuǎn)換:10.623
分享:PHP技巧:php過濾危險(xiǎn)html代碼用PHP過濾html里可能被利用來引入外部危險(xiǎn)內(nèi)容的代碼。有些時(shí)候,需要讓用戶提交html內(nèi)容,以便豐富用戶發(fā)布的信息,當(dāng)然,有些可能造成顯示頁面布局混亂的代碼也在過濾范圍內(nèi)。
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)
- 教程說明:
PHP教程-php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)
。