Home > php教程 > php手册 > php的汉字转换: GBK->Big5

php的汉字转换: GBK->Big5

WBOY
Release: 2016-06-21 09:06:15
Original
951 people have browsed it

汉字|转换

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);
 }
 
 function qswhDir($dirname,$fail="??"){
  /******(qiushuiwuhen 2002-9-6)******/
  $d = dir($dirname);
  while($entry=$d->read()) {
   if($entry=="."||$entry=="..")continue;
   $entry=$dirname."/".$entry;
   if(is_dir($entry))
       $this->qswhDir($entry,$fail);
      else{
       if(substr($entry,-5)!=".qswh")$this->qswhFile($entry,$fail);
      }
  }
  $d->close();
  return $c;
 }
 
}


使用范例:
$words="秋水无恨";
$qswh=new qswhBig5("qswhBig5.php");//qswhBig5.php,可省参数

echo("

请选择编码Big5查看:".$qswh->qswhStr($words));<br>echo("\n编码一目录:".$qswh->qswhDir("test"));<br>echo("\n编码一文件:".$qswh->qswhFile("index.htm"));<p><br>使用说明:<br>qswhStr中第二个参数代表如果没有对应的Big5将替换的字,如果为空,则保留该字<br>将文件格式化后,记得要修改<meta></p> <p></p>
 



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template