php的漢字轉換:GBK至Big5

來源:互聯網
上載者:User
P>php的漢字轉換一直是比較麻煩的事
首先要說兩點:
1.GBK的繁體字並不是Big5,但一般繁體字都有對應的Big碼
2.一GBK碼並不一定有對應的Big5碼,所以一般是無法復原轉換
該類內建了三個函數
qswhStr 格式化字串
qswhFile 格式化一檔案
qswhDir 格式化目錄下所有檔案(.qswh的除外,那是原先文檔的備份)
qswhBig5.php 從這裡下載
/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<strlen($tmp);$j+=6)if(($k=hexdec(substr($tmp,$j,2)))>=$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);
 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.