PHP Chinese character conversion GBK-Big5_PHP tutorial

WBOY
Release: 2016-07-13 17:25:34
Original
949 people have browsed it


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 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;
}
}
Usage example:
$words="Qiushui Wuhen";
$qswh=new qswhBig5("qswhBig5.php ");//qswhBig5.php, you can save parameters
echo("Please select the encoding Big5 to view: ".$qswh->qswhStr($words));<br>echo(" Encoding a directory: ".$qswh->qswhDir("test"));<br>echo(" Encode a file: ".$qswh->qswhFile("index.htm"));<br>Instructions for use: <br>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 <br> After formatting the file, remember to modify it <meta> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/532057.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/532057.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">PHP’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...</span> </div>

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!