您好,欢迎访问一九零五行业门户网

php对gb编码动态转utf-8编码的几种方法评测_PHP教程

在《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地址->地理位置转换的测评》一文):
> 6);
$str .= chr(0x80 | $c & 0x3f);
} elseif ($c $str .= chr(0xe0 | $c >> 12);
$str .= chr(0x80 | $c >> 6 & 0x3f);
$str .= chr(0x80 | $c & 0x3f);
} elseif ($c $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 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 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 $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 $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 $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
http://www.bkjia.com/phpjc/364036.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/364036.htmltecharticle在《ip地址-地理位置转换的测评》一文中提到用ip2addr函数直接读取ip数据库文件是效率最高的,相比用mysql数据库存储ip数据,用sql查询是效...
其它类似信息

推荐信息