Chinese character conversion in php has always been a troublesome thing
First of all, there are two points:
1. The traditional Chinese characters in GBK are not Big5, but generally traditional Chinese characters have corresponding Big codes
2. A GBK code does not necessarily have a corresponding Big5 code, so it is generally an irreversible conversion
This class has three built-in functions
qswhStr format string
qswhFile format a file
qswhDir format directory Download all files (except .qswh, which is a backup of the original document)
qswhBig5.php Download from here
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
$q=ord(substr($gb,+ +$i,1));
$tmp=$this->qswhData[$p-128];
for($j=0;$j
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;
}
}
Usage example:
$words="Qiushui Wuhen";
$qswh=new qswhBig5("qswhBig5.php ");//qswhBig5.php, you can save parameters
echo("
echo("
Encoding a directory: ".$qswh->qswhDir("test"));
echo("
Encode a file: ".$qswh->qswhFile("index.htm"));
Instructions for use:
The second parameter in qswhStr represents the word that Big5 will replace if there is no corresponding one. If it is empty, Then keep this word
After formatting the file, remember to modify it