Home  >  Article  >  Backend Development  >  Chinese character conversion in php: GBK to Big5_PHP tutorial

Chinese character conversion in php: GBK to Big5_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:181047browse

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=$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);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445226.htmlTechArticlePphp’s Chinese character conversion has always been a troublesome thing. First of all, I have to say two points: 1. The traditional Chinese characters of 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...
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