程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php的漢字轉換GBK-Big5

php的漢字轉換GBK-Big5

編輯:關於PHP編程


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> <div class="qcxs_ie_he"></div> </div> <div class="akevx_own"><script language='javascript' src='https://www.aspphp.online/AD/apnebex/m4.js'></script></div> <div class="akevx_mj"></div> <div class="qcxs_ie_he"></div> <ol class="qmsbvx_hsb ovv_hex"> <li>上一頁:<a class='LinkPrevArticle' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/150571.html' >ZendOptimizer配置指南</a></li><li>下一頁:<a class='LinkNextArticle' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/150573.html' >php的漢字轉換GBK-UnicodeUTF8</a></li> </ol> </div> </div> <div class="qirnxbd bg1 mt20"> <div class="jdbsbav"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/' target="_blank">關於PHP編程</a> <div class="jdbsbav_hx"></div> </div> <div class="qirnxbd_rbx"> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156209.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011718003287.gif' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156209.html' target="_blank"><strong>【PHP基礎知識】$GLOBALS[]和global區別</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156209.html' target="_blank"><p>在php程序開發中,很多開發者沒有注意到$GLOBALS[]</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139874.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011617320409.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139874.html' target="_blank"><strong>thinkPHP中分頁用法實例分析,</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139874.html' target="_blank"><p>thinkPHP中分頁用法實例分析,本文實例講述了think</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141479.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709054458.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141479.html' target="_blank"><strong>PHP設計模式——迭代器模式</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141479.html' target="_blank"><p>PHP設計模式——迭代器模式 &nbsp; 迭代器模式:</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/144752.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011710332326.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/144752.html' target="_blank"><strong>PHP練手:日歷(代碼簡單,擴展容易)</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/144752.html' target="_blank"><p>PHP練手:日歷(代碼簡單,擴展容易) </p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142071.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709162304.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142071.html' target="_blank"><strong>2015 年最好的 PHP 框架調查統計</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142071.html' target="_blank"><p>2015 年最好的 PHP 框架調查統計 首先第一件事情,如</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141885.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709132734_S.jpg' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141885.html' target="_blank"><strong>php模擬用戶自動在qq空間發表文章的方法</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141885.html' target="_blank"><p>php模擬用戶自動在qq空間發表文章的方法 我們這裡是一個簡</p></a> </div> <div class="qcxs_ie_he"></div> </div> </div> </div> <div class="eccsgxsg mt20"> <div class="xiangbgbax bg1"> <div class="ejmnxbns">相關文章</div> <ul class="pdirbvx lbhevs ovv_hex lbhevs_40"> <li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231982.html' target="_self">PHP獲取用戶的真實IP地址</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231981.html' target="_self">centos&nbsp;7下如何定時執行php腳本</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231980.html' target="_self">php操作mysql數據庫的方式</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231969.html' target="_self">PHP基礎學習</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231964.html' target="_self">php正則表達式</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231963.html' target="_self">PHP如何自動轉義和還原字符串</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231962.html' target="_self">PHP截取字符串的例子</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231961.html' target="_self">PHP如何檢測密碼長度</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231960.html' target="_self">php異常處理</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/gengduo/delphi/201704/231846.html' target="_self">Delphi7.0常用函數-屬性-事件,delphi7.0函數</a></li> </ul> </div> <div class="akevx_oin"></div> <div class="nenxnxb bg1 mt20"> <div class="enxvsfgg"> <div class="enxvsfgg_1">閱讀排行榜</div> </div> <div class="pdirbvx lbhevs ovv_hex"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/148263.html' target="_blank">php baidu 收錄查詢程序</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139268.html' target="_blank">Memcached 視頻教程 大數據 高性能 集群 NoSQL 安裝 命令 使用,memcachednosql</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141826.html' target="_blank">ThinkPHP整合Smarty模板引擎的使用以及查詢方法匯總</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143821.html' target="_blank">PHPEMS在線模擬考試系統源碼,phpems模擬考試系統</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/147498.html' target="_blank">php判斷變量是否是整數的兩種方法</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/153558.html' target="_blank">如何學習一個新的PHP框架</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/159699.html' target="_blank">PHP 面向對象改進後的一點說明第1/2頁</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142274.html' target="_blank">使用php轉義輸出HTML到JavaScript,轉義javascript</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156812.html' target="_blank">php魔術方法與魔術變量、內置方法與內置變量的深入分析</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/161351.html' target="_blank">Oracle Faq(Oracle TAF的配置)</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/157222.html' target="_blank">php excel reader讀取excel內容存入數據庫實現代碼</a> </div> </div> <div class="wuwbxbxsv mt20"> <div class="hrbbxsxsf">熱門圖文</div> <div class="sbnexs"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/155929.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011717573991.gif' width='230' height='170' border='0'></a> </div> <div class="pdirbvx lbhevs ovv_hex lbhevs_80"> <a href='https://www.aspphp.online/bianchen/cyuyan/cjj/cjjrm/201701/213137.html' target="_blank">C++ Memento模式應用法則</a> <a href='https://www.aspphp.online/bianchen/dnet/cxiapu/gycxp/201701/10904.html' target="_blank">C#中New關鍵詞的幾種用法</a> <a href='https://www.aspphp.online/bianchen/cyuyan/cjj/gycjj/201701/4927.html' target="_blank">Best Time to Buy and Sell Stock</a> <a href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201701/62759.html' target="_blank">Drupal7連接多個數據庫及常見問題解決</a> <a href='https://www.aspphp.online/bianchen/dnet/cxiapu/cxpjc/201701/123361.html' target="_blank">C# 文字代碼頁 文字編碼的代碼頁名稱速查表</a> <a href='https://www.aspphp.online/bianchen/java/javazh/201701/165810.html' target="_blank">python如何查看線程id</a> <a href='https://www.aspphp.online/bianchen/cyuyan/vcjj/gyvcplus/201701/8734.html' target="_blank">用VC++開發WinPopup的增強版</a> <a href='https://www.aspphp.online/bianchen/dnet/dnetsl/201701/104282.html' target="_blank">c#.net下簡單的Ajax例子</a> </div> </div> <div class="eqibneb bg1 mt20"> <div class="lkebxsg">欄目導航</div> <div class="eqibneb_1 ovv_hex"> <a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phpjc/' target="_self">PHP基礎知識</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/' target="_self">PHP綜合</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phprm/' target="_self">PHP入門知識</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/' target="_self">關於PHP編程</a> </div> </div> </div> <div class="qcxs_ie_he"></div> </div> </div> <div class="qcxs_ie_he"></div> <div class="ebrxbsbf">Copyright &copy; <a href="https://www.aspphp.online/" target="_blank" title="程式師世界">程式師世界</a> All Rights Reserved </div> </body> </html>