设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 网络编程 >> PHP文摘 >> PHP对GB编码动态转UTF-8几种方法评测
精品推荐
PHP文摘点击TOP10
·谈网页编程PHP语言的发展
·PHP 脚本:随心所欲的代码逐渐流行
·php4和php5区别
·针对初学PHP者的疑难问答(1)
·初探PHP5
·PHP4在Windows2000下的安装
·真正面向对象编程:PHP5.01发布
·PHP安装攻略:常见问题解答(二)
·PHP安装攻略:常见问题解答(一)
·Zend Studio 5 Beta
网络编程点击TOP10
·ASP.NET 程序中常用的三十三种代码
·用C#实现木马程序
·C#编写的windows计算器-源代码
·从零开始学ASP.NET(基础篇)
·VS.net 2005 Beta 下载地址(Express Edition)
·利用Web Application Stress Tool(WAS)做性能测试(1)
·"SQL Server不存在或访问被拒绝"问题的解决
·《用C#和数据库实现无限级分类法》修正程序
·.NET 初 级 读 本
·我的.NET书架 (入门篇)
精选专题

PHP对GB编码动态转UTF-8几种方法评测

作者: 来源:http://www.xgdown.com/ 时间:2007-9-9 10:58:17

PHP对GB编码动态转UTF-8几种方法评测(4)

<?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;
         }
共5页 9 7 [1] [2] [3] [4] [58 :>

PHP对GB编码动态转UTF-8几种方法评测 相关文章:
PHP对GB编码动态转UTF-8几种方法评测 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com