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("編碼一目錄:".$qswh->qswhDir("test"));<br>echo("編碼一檔案:".$qswh->qswhFile("index.htm"));<br>使用說明:<br>qswhStr中第二個參數代表如果沒有對應的Big5將替換的字,如果為空白,則保留該字<br>將檔案格式化後,記得要修改<meta中的content-type為Big。ps.該文章現僅限在csdn文檔中心,如要轉載,請和作者聯絡,否則後果自負。</p><p align="left"><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的漢字轉換一直是比較麻煩的事 首先要說兩點: 1.GBK的繁體字並不是Big5,但一般繁體字都有對應的Big碼 2.一GBK碼並不一定有對應的Big5碼...</span></p><li ><i class="layui-icon">