Home  >  Article  >  php教程  >  php的汉字转换:GBK至Big5

php的汉字转换:GBK至Big5

WBOY
WBOYOriginal
2016-06-13 11:20:221078browse

P>php的汉字转换一直是比较麻烦的事
首先要说两点:
1.GBK的繁体字并不是Big5,但一般繁体字都有对应的Big码
2.一GBK码并不一定有对应的Big5码,所以一般是不可逆转换
该类内置了三个函数
qswhStr 格式化字符串
qswhFile 格式化一文件
qswhDir 格式化目录下所有文件(.qswh的除外,那是原先文档的备份)
qswhBig5.php 从这里下载
http://www.blueidea.com/user/qswh/qswhBig5.zip

class qswhBig5{
 var $qswhData;
 function qswhBig5($filename="qswhBig5.php"){
  $this->qswhData=file($filename);
 }
 function qswhStr($gb,$fail="??"){
  /******(qiushuiwuhen 2002-9-6)******/
  $ret="";
  for($i=0;$i     if(($p=ord(substr($gb,$i,1)))>127){
      $q=ord(substr($gb,++$i,1));
      $tmp=$this->qswhData[$p-128];
      for($j=0;$j=$q)break;
      if($k==$q)$q=chr(hexdec(substr($tmp,$j+2,2))).chr(hexdec(substr($tmp,$j+4,2)));
      else if($fail=="")$q=chr($p).chr($q); else $q=$fail;
     }
     else
      $q=chr($p);
     $ret.=$q;
  }
  return $ret;
 }
 function qswhFile($filename,$fail="??"){
  /******(qiushuiwuhen 2002-9-6)******/
  if(!file_exists($filename.".qswh"))copy($filename,$filename.".qswh");
  $fp=fopen($filename,"r+");
  $tmp=fread($fp,filesize($filename));
  rewind($fp);
  fwrite($fp,$this->qswhStr($tmp,$fail));
  fclose($fp);
 }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:用 PHP 实现 POP3 邮件的解码(1)Next article:多重