用php實現 gb2312 和 unicode 間的編碼轉換

來源:互聯網
上載者:User
編碼|轉換

gb2312 和 unicode 間的編碼轉換

下面的例子是將 gb2312 轉換為 "全"這種形式

php4.3.1以後的iconv函數很好用的,只是需要自己寫一個uft8到unicode的轉換函式
查表(gb2312.txt)也行
<?
$text = "電子書庫";
preg_match_all("/[\x80-\xff]?./",$text,$ar);
foreach($ar[0] as $v)
  echo "&#".utf8_unicode(iconv("GB2312","UTF-8",$v)).";";
?>
<?
// utf8 -> unicode
function utf8_unicode($c) {
  switch(strlen($c)) {
    case 1:
      return ord($c);
    case 2:
      $n = (ord($c[0]) & 0x3f) << 6;
      $n += ord($c[1]) & 0x3f;
      return $n;
    case 3:
      $n = (ord($c[0]) & 0x1f) << 12;
      $n += (ord($c[1]) & 0x3f) << 6;
      $n += ord($c[2]) & 0x3f;
      return $n;
    case 4:
      $n = (ord($c[0]) & 0x0f) << 18;
      $n += (ord($c[1]) & 0x3f) << 12;
      $n += (ord($c[2]) & 0x3f) << 6;
      $n += ord($c[3]) & 0x3f;
      return $n;
  }
}
?>


下面的例子是利用php將"全"這中編碼轉換為gb2312.
<?php
$str = "TTL全天候自动聚焦";
$str = preg_replace("|&#([0-9]{1,5});|", "\".u2utf82gb(\\1).\"", $str);
$str = "\$str=\"$str\";";

eval($str);
echo $str;

function u2utf82gb($c){
    $str="";
    if ($c < 0x80) {
         $str.=$c;
    } else if ($c < 0x800) {
         $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
         $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x200000) {
         $str.=chr(0xF0 | $c>>18);
         $str.=chr(0x80 | $c>>12 & 0x3F);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    }
    return iconv('UTF-8', 'GB2312', $str);
}
?>

或者是

function unescape($str) {
  $str = rawurldecode($str);
  preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
  $ar = $r[0];
print_r($ar);
  foreach($ar as $k=>$v) {
    if(substr($v,0,2) == "%u")
      $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
    elseif(substr($v,0,3) == "&#x")
      $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
    elseif(substr($v,0,2) == "&#") {
echo substr($v,2,-1)."<br>";
      $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
    }
  }
  return join("",$ar);
}

$str = "TTL全天候自动聚焦";
echo unescape($str); //out TTL全天候自動聚焦

利用javascript來轉換

<style>
BODY {
 FONT-SIZE: 9pt; PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px;
}
input {
 FONT-SIZE: 9pt; height: 13pt;
}
</style>

<script language="JavaScript1.2">
/*
 This following code are designed and writen by Windy_sk <seasonx@163.net>
 You can use it freely, but u must held all the copyright items!
*/

function Str2Unicode(str){
 var arr = new Array();
 for(var i=0;i<str.length;i++){
  arr[i]="&#" + str.charCodeAt(i) + ";";
 }
 return(arr.toString().replace(/,/g,""));
}

function Unicode2oStr(str){
 var re=/&#[\da-fA-F]{1,5};/ig;
 var arr=str.match(re);
 if(arr==null)return("");
 for(var i=0;i<arr.length;i++){
  arr[i]=String.fromCharCode(arr[i].replace(/[&#;]/g,""));
 }
 return(arr.toString().replace(/,/g,""))
}

function modi_str(){
 if(document.all.text.method.checked){
  if(document.all.text.decode.value!=""){
   document.all.text.encode.value = Str2Unicode(document.all.text.decode.value);
  }else{
   document.all.text.decode.value = Unicode2oStr(document.all.text.encode.value);
  }
 }else{
  if(document.all.text.encode.value!=""){
   document.all.text.decode.value = Unicode2oStr(document.all.text.encode.value);
  }else{
   document.all.text.encode.value = Str2Unicode(document.all.text.decode.value);
  }
 }
}
</script>
<title>Unicode</title>
<form name=text>
文本原型:<br>
  <textarea name="decode" cols="100" rows="10"></textarea>
  <br>
轉碼:<br>
  <textarea name="encode" cols="100" rows="10"></textarea>
  <br>
  <input type="checkbox" name="method" checked> 正向轉換
  <input type=button value=" 確 定 ">
  <input type=reset value=" 清 空 ">
  <input type=button value=" 全 選 ">
</form>


下面是一個顯示所有全形半形的字型的查看例子

<style>
BODY {
 FONT-SIZE: 9pt; PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px;
}
input {
 FONT-SIZE: 9pt; height: 13pt;
}
</style>
<script>
function showUni(min,max){
 show.document.open();
 show.document.writeln("<style>body{font-size:9pt;word-break:break-all;}</style>");
 show.document.writeln(min + " - " + max + "<br><br>");
 var i=0;
 for(i=min;i<=max;i++){
  show.document.write("&#" + i + ";");
 }
 show.document.close();
}
</script>
<input type=button value="半形"  onclick=showUni(32,126)>
<input type=button value="全形"  onclick=showUni(65281,65374)>
<input type=button value="中文1" onclick=showUni(19968,40869)>
<input type=button value="中文2" onclick=showUni(63744,64045)>
<input type=button value="日文平"  onclick=showUni(12353,12435)>
<input type=button value="日文片"  onclick=showUni(12449,12534)>
<input type=button value="韓文"  onclick=showUni(44032,55203)>
<br>自訂:<input name=min> - <input name=max>
<input type=button value="察看" onclick=showUni(parseInt(document.all.min.value),parseInt(document.all.max.value))>
<br>
<iframe src="about:blank" id=show width=100% height=70% scroll=no></iframe>


下面是一個查表(gb2312),轉換gb2312到utf8的例子, 現在有iconv函數,這個已經沒有太大的意義了,

<?
function gb2utf8($gb){
    if(!trim($gb))  return $gb;
    $filename="gb2312.txt";
    $tmp=file($filename);
    $codetable=array();
    while(list($key,$value)=each($tmp))
          $codetable[hexdec(substr($value,0,6))]=substr($value,7,6);

    $utf8="";
    while($gb) {
        if (ord(substr($gb,0,1))>127) {
          $this=substr($gb,0,2);
          $gb=substr($gb,2,strlen($gb)-2);
          $utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
        }else{
            $this=substr($gb,0,1);
            $gb=substr($gb,1,strlen($gb)-1);
            $utf8.=u2utf8($this);
        }
    }
    return $utf8;
}

function u2utf8($c){
    $str="";
    if ($c < 0x80) {
         $str.=$c;
    } else if ($c < 0x800) {
         $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
         $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x200000) {
         $str.=chr(0xF0 | $c>>18);
         $str.=chr(0x80 | $c>>12 & 0x3F);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    }
    return $str;
}

?>


 



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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