php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)(2)_PHP教程
推薦:php配置文件php.ini的中文注釋版這個(gè)文件控制了PHP許多方面的觀點(diǎn)。為了讓PHP讀取這個(gè)文件,它必須被命名為 ; ´php.ini´。PHP 將在這些地方依次查找該文件:當(dāng)前工作目錄;環(huán)境變量PHPRC ; 指明的路徑;
可見這次是MySQL方法遙遙領(lǐng)先于文件查詢法。但是現(xiàn)在還不急于使用MySQL方法,因?yàn)槲谋疚募椒ㄖ匀绱撕臅r(shí),主要因?yàn)樗看无D(zhuǎn)換都要把整個(gè)gb_unicode.txt讀入內(nèi)存,而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> |
文本文件效率較低,于是考慮把文本文件轉(zhuǎn)換為二進(jìn)制文件,然后用折半法查找這個(gè)文件,而不需要把整個(gè)文件讀入內(nèi)存。文件格式為:文件頭2字節(jié),存儲(chǔ)記錄數(shù);接著一條接一條記錄存入文件,每條記錄4字節(jié),前2字節(jié)對(duì)應(yīng)GB代碼,后2字節(jié)對(duì)應(yīng)Unicode代碼。轉(zhuǎn)換程序如下:
<?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); ?> |
執(zhí)行程序后就獲得了二進(jìn)制的GB->Unicode對(duì)照表gbu.dat,并且數(shù)據(jù)記錄按GB代碼排了序,便于折半法查找。使用gbu.dat進(jìn)行轉(zhuǎn)碼的函數(shù)如下:
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; } |
把其加到原來(lái)的測(cè)評(píng)程序,對(duì)三種方法同時(shí)測(cè)評(píng)2次得到數(shù)據(jù)(精確到3位小數(shù),單位:秒):
MySQL方法:0.125
文本文件方法:10.873
二進(jìn)制文件折半法:0.106
MySQL方法:0.102
文本文件方法:10.677
二進(jìn)制文件折半法:0.092
分享:PHP技巧:php過(guò)濾危險(xiǎn)html代碼用PHP過(guò)濾html里可能被利用來(lái)引入外部危險(xiǎn)內(nèi)容的代碼。有些時(shí)候,需要讓用戶提交html內(nèi)容,以便豐富用戶發(fā)布的信息,當(dāng)然,有些可能造成顯示頁(yè)面布局混亂的代碼也在過(guò)濾范圍內(nèi)。
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- php匹配字符中鏈接地址程序代碼
- PHP_include文件出錯(cuò)的解決方法
- PHP生成隨機(jī)字符串
- 用PHP生成中文拼音代碼
- Windows IIS環(huán)境下安裝和配置PHP開發(fā)環(huán)境
- PHP中的Sessions簡(jiǎn)單學(xué)習(xí)動(dòng)態(tài)網(wǎng)頁(yè)制作
- 深入探討:Nginx 502 Bad Gateway錯(cuò)誤的解決方法
- 深入解析PHP中的(偽)多線程與多進(jìn)程
- PHP在Web開發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?
- 用PHP程序?qū)崿F(xiàn)刪除目錄的三種方法實(shí)例
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)(2)
- 教程說(shuō)明:
PHP教程-php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)(2)
。