喜欢本站并加入收藏
首 页 电脑学园 技术文档 图形图像 办公应用 网页设计 管理资料 PPT模板 方案合同
 位置: 北方教程网 >> 网页设计 >> PHP专题 >> 正文
   PHP中 Big5 Utf-8 GB2312相互转码的完美解决
PHP中 Big5 Utf-8 GB2312相互转码的完美解决
[ 作者:佚名   来源:网络整理   点击数:   更新时间:2007-5-23 ]
大家一定在编写PHP代码的过程中,经常会遇到需要对中文转码的问题,如 GB2312 <=> Unicode、GB2312 <=> Big5 等等。如果 PHP 编译时带有 mbstring 的话,可以使用 Multi-Byte String Function 实现部分转码工作。然而由于很多虚拟主机不支持 mbstring,或者 mbstring 的编译、配置过于麻烦,很多 PHP 代码无法使用这一序列的函数。

最近为了解决这个问题,找到一个不错的项目:PHP News Reader,这是一个基于 WEB 的新闻阅读器,支持基于 NNTP (RFC 977) 协议的新闻文章的阅读、发布、删除、回复等功能。这个项目实现了 GB2312 Big5 Unicode(UTF-8) 之间的相互转码,这个正是我所关心的部分。

使用 CVS 客户端(Linux 下直接用命令行就行,Windows 下推荐使用 Tortoise CVS)将项目的代码 Check Out 出来:

# cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/pnews loginLogging in to :pserver:anonymous@cvs.sourceforge.net:2401/cvsroot/pnewsCVS password: (Press Enter)# cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/pnews co pnewscvs server: Updating pnews…

查看 pnews/language 目录,此目录下包含了如下文件:

big5-gb.tabbig5-unicode.tabgb-big5.tabgb-unicode.tabunicode-big5.tabunicode-gb.tab

这些都是用于字符转换的码表,然后再看看 pnews/language.inc.php 文件,其中包含了几个用于编码转换的函数:

// Big5 => GBfunction b2g( $instr ) { $fp = fopen( 'language/big5-gb.tab', 'r' ); $len = strlen($instr); for( $i = 0 ; $i < $len ; $i++ ) {  $h = ord($instr[$i]);  if( $h >= 160 ) {   $l = ord($instr[$i+1]);   if( $h == 161 && $l == 64 )    $gb = '  ';   else {    fseek( $fp, (($h-160)*255+$l-1)*3 );    $gb = fread( $fp, 2 );   }   $instr[$i] = $gb[0];   $instr[$i+1] = $gb[1];   $i++;  } } fclose($fp); return $instr;}// GB => BIG5function g2b( $instr ) { $fp = fopen( 'language/gb-big5.tab', 'r' ); $len = strlen($instr); for( $i = 0 ; $i < $len ; $i++ ) {  $h = ord($instr[$i]);  if( $h > 160 && $h < 248 ) {   $l = ord($instr[$i+1]);   if( $l > 160 && $l < 255 ) {    fseek( $fp, (($h-161)*94+$l-161)*3 );    $bg = fread( $fp, 2 );   }   else    $bg = '  ';   $instr[$i] = $bg[0];   $instr[$i+1] = $bg[1];   $i++;  } } fclose($fp); return $instr;}// Big5 => Unicode(UtF-8)function b2u( $instr ) { $fp = fopen( 'language/big5-unicode.tab', 'r' ); $len = strlen($instr); $outstr = ''; for( $i = $x = 0 ; $i < $len ; $i++ ) {  $h = ord($instr[$i]);  if( $h >= 160 ) {   $l = ord($instr[$i+1]);   if( $h == 161 && $l == 64 )    $uni = '  ';   else {    fseek( $fp, ($h-160)*510+($l-1)*2 );    $uni = fread( $fp, 2 );   }   $codenum = ord($uni[0])*256 + ord($uni[1]);   if( $codenum < 0x800 ) {    $outstr[$x++] = chr( 192 + $codenum / 64 );    $outstr[$x++] = chr( 128 + $codenum % 64 );#    printf("[%02X%02X]<br>n", ord($outstr[$x-2]), ord($uni[$x-1]) );   }   else {    $outstr[$x++] = chr( 224 + $codenum / 4096 );    $codenum %= 4096;    $outstr[$x++] = chr( 128 + $codenum / 64 );    $outstr[$x++] = chr( 128 + ($codenum % 64) );#    printf("[%02X%02X%02X]<br>n", ord($outstr[$x-3]), ord($outstr[$x-2]), ord($outstr[$x-1]) );   }   $i++;  }  else   $outstr[$x++] = $instr[$i]; } fclose($fp); if( $instr != '' )  return join( '', $outstr);}// Unicode(UTF-8) => BIG5function u2b( $instr ) { $fp = fopen( 'language/unicode-big5.tab', 'r' ); $len = strlen($instr); $outstr = ''; for( $i = $x = 0 ; $i < $len ; $i++ ) {  $b1 = ord($instr[$i]);  if( $b1 < 0x80 ) {   $outstr[$x++] = chr($b1);#   printf( "[%02X]", $b1);  }  elseif( $b1 >= 224 ) { # 3 bytes UTF-8   $b1 -= 224;   $b2 = ord($instr[$i+1]) - 128;   $b3 = ord($instr[$i+2]) - 128;   $i += 2;   $uc = $b1 * 4096 + $b2 * 64 + $b3 ;   fseek( $fp, $uc * 2 );   $bg = fread( $fp, 2 );   $outstr[$x++] = $bg[0];   $outstr[$x++] = $bg[1];#   printf( "[%02X%02X]", ord($bg[0]), ord($bg[1]));  }  elseif( $b1 >= 192 ) { # 2 bytes UTF-8   printf( "[%02X%02X]", $b1, ord($instr[$i+1]) );   $b1 -= 192;   $b2 = ord($instr[$i]) - 128;   $i++;   $uc = $b1 * 64 + $b2 ;   fseek( $fp, $uc * 2 );   $bg = fread( $fp, 2 );   $outstr[$x++] = $bg[0];   $outstr[$x++] = $bg[1];#   printf( "[%02X%02X]", ord($bg[0]), ord($bg[1]));  } } fclose($fp); if( $instr != '' ) {#  echo '##' . $instr . " becomes " . join( '', $outstr) . "<br>n";  return join( '', $outstr); }}// GB => Unicode(UTF-8)function g2u( $instr ) { $fp = fopen( 'language/gb-unicode.tab', 'r' ); $len = strlen($instr); $outstr = ''; for( $i = $x = 0 ; $i < $len ; $i++ ) {  $h = ord($instr[$i]);  if( $h > 160 ) {   $l = ord($instr[$i+1]);   fseek( $fp, ($h-161)*188+($l-161)*2 );   $uni = fread( $fp, 2 );   $codenum = ord($uni[0])*256 + ord($uni[1]);   if( $codenum < 0x800 ) {    $outstr[$x++] = chr( 192 + $codenum / 64 );    $outstr[$x++] = chr( 128 + $codenum % 64 );#    printf("[%02X%02X]<br>n", ord($outstr[$x-2]), ord($uni[$x-1]) );   }   else {    $outstr[$x++] = chr( 224 + $codenum / 4096 );    $codenum %= 4096;    $outstr[$x++] = chr( 128 + $codenum / 64 );    $outstr[$x++] = chr( 128 + ($codenum % 64) );#    printf("[%02X%02X%02X]<br>n", ord($outstr[$x-3]), ord($outstr[$x-2]), ord($outstr[$x-1]) );   }   $i++;  }  else   $outstr[$x++] = $instr[$i]; } fclose($fp); if( $instr != '' )  return join( '', $outstr);}// Unicode(UTF-8) => GBfunction u2g( $instr ) { $fp = fopen( 'language/unicode-gb.tab', 'r' ); $len = strlen($instr); $outstr = ''; for( $i = $x = 0 ; $i < $len ; $i++ ) {  $b1 = ord($instr[$i]);  if( $b1 < 0x80 ) {   $outstr[$x++] = chr($b1);#   printf( "[%02X]", $b1);  }  elseif( $b1 >= 224 ) { # 3 bytes UTF-8   $b1 -= 224;   $b2 = ord($instr[$i+1]) - 128;   $b3 = ord($instr[$i+2]) - 128;   $i += 2;   $uc = $b1 * 4096 + $b2 * 64 + $b3 ;   fseek( $fp, $uc * 2 );   $gb = fread( $fp, 2 );   $outstr[$x++] = $gb[0];   $outstr[$x++] = $gb[1];#   printf( "[%02X%02X]", ord($gb[0]), ord($gb[1]));  }  elseif( $b1 >= 192 ) { # 2 bytes UTF-8   printf( "[%02X%02X]", $b1, ord($instr[$i+1]) );   $b1 -= 192;   $b2 = ord($instr[$i]) - 128;   $i++;   $uc = $b1 * 64 + $b2 ;   fseek( $fp, $uc * 2 );   $gb = fread( $fp, 2 );   $outstr[$x++] = $gb[0];   $outstr[$x++] = $gb[1];#   printf( "[%02X%02X]", ord($gb[0]), ord($gb[1]));  } } fclose($fp); if( $instr != '' ) {#  echo '##' . $instr . " becomes " . join( '', $outstr) . "<br>n";  return join( '', $outstr); }}

在自己的 PHP 文件中需要转码时,只需要 .tab 码表文件及相对应的转码函数,将函数中的 fopen 打开的文件路径修改为正确的路径即可。

上一篇文章: 利用PHP制作超酷数据饼图代码 下一篇文章: 详解PHP中的foreach循环
打印此文  收藏此页  关闭窗口  返回顶部
 相关文章  热点文章
 · 利用CSS改进网站设计的小技巧
 · Dreamweaver快捷操作技巧集锦
 · 使用Dreamweaver架设网站简明教程
 · 网页设计制作过程中常用的几个代码技巧
 · 高效使用CSS编码的方法
 
热点图文
教你节省Vist...
使用Windows ...
 · 远程管理Web服务器的方法技巧
 · Win Vista系统自带IIS7.0设置详解...
 · 详解Windows系统IIS6.0功能及应用...
 · 服务器集群系统实现方法及案例分...
 · IIS服务器实现asp,cgi和php+mysq...
 · IIS下SQL Server数据库安全设置技...
推 荐 阅 读
巧用Photosho...
Photoshop鼠绘...
 · Fireworks制作旋转的漂亮gif动画...
 · Photoshop制作都市满天繁星
 · Fireworks制作Menu按钮
 · 巧用Photoshop制作喜迎奥运壁纸
 · 巧用Photoshop钢笔工具绘制彩色美...
 · Photoshop制作浪漫动感花朵
 设为首页 - 加入收藏 - 关于本站 - 联系我们 - 广告合作 - 友情链接 - 在线交流 - 
CopyRight© 2006-2009 Eduai.Com All Rights Reserved
在线交流 QQ:524152258 辽ICP备07001851号
免责声明:本站部分资源来自网络,如有侵犯您的版权请通知,我们立即删除。