Home >Backend Development >PHP Tutorial >Chinese character conversion in php: GBK to Big5_PHP tutorial
P>PHP’s Chinese character conversion 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. 1. GBK code does not necessarily have a corresponding Big5 code, so it is generally irreversible conversion
This class has three built-in functions
qswhStr format string
qswhFile format a file
qswhDir format directory 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< strlen($gb);$i++){
if(($p=ord(substr($gb,$i,1)))>127){
$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);
}