熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

用PHP實現GB2312和Unicode間的編碼轉換

2013-11-15 12:22:06  來源: PHP編程 

  gb 和 unicode 間的編碼轉換
  
  下面的例子是將 gb 轉換為 &#;這種形式
  
  php以後的iconv函數很好用的只是需要自己寫一個uft到unicode的轉換函數
  查表(gbtxt)也行
  <?
  $text = 電子書庫;
  preg_match_all(/[\x\xff]?/$text$ar);
  foreach($ar[] as $v)
  echo &#utf_unicode(iconv(GBUTF$v));;
  ?>
  <?
  // utf > unicode
  function utf_unicode($c) {
  switch(strlen($c)) {
  case :
  return ord($c);
  case :
  $n = (ord($c[]) & xf) << ;
  $n += ord($c[]) & xf;
  return $n;
  case :
  $n = (ord($c[]) & xf) << ;
  $n += (ord($c[]) & xf) << ;
  $n += ord($c[]) & xf;
  return $n;
  case :
  $n = (ord($c[]) & xf) << ;
  $n += (ord($c[]) & xf) << ;
  $n += (ord($c[]) & xf) << ;
  $n += ord($c[]) & xf;
  return $n;
  }
  }
  ?>
  
  下面的例子是利用php將&#;這中編碼轉換為gb
  <?php
  $str = TTL&#;&#;&#;&#;&#;&#;&#;;
  $str = preg_replace(|&#([]{});| \uutfgb(\\)\ $str);
  $str = \$str=\$str\;;
  
  eval($str);
  echo $str;
  
  function uutfgb($c){
  $str=;
  if ($c < x) {
  $str=$c;
  } else if ($c < x) {
  $str=chr(xC | $c>>);
  $str=chr(x | $c & xF);
  } else if ($c < x) {
  $str=chr(xE | $c>>);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c & xF);
  } else if ($c < x) {
  $str=chr(xF | $c>>);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c & xF);
  }
  return iconv(UTF GB $str);
  }
  ?>
  
  或者是
  
  function unescape($str) {
  $str = rawurldecode($str);
  preg_match_all(/(?:%u{})|&#x{};|&#\d+;|+/U$str$r);
  $ar = $r[];
  print_r($ar);
  foreach($ar as $k=>$v) {
  if(substr($v) == %u)
  $ar[$k] = iconv(UCSGBpack(Hsubstr($v)));
  elseif(substr($v) == &#x)
  $ar[$k] = iconv(UCSGBpack(Hsubstr($v)));
  elseif(substr($v) == &#) {
  echo substr($v)<br>;
  $ar[$k] = iconv(UCSGBpack(nsubstr($v)));
  }
  }
  return join($ar);
  }
  
  $str = TTL&#;&#;&#;&#;&#;&#;&#;;
  echo unescape($str); //out TTL全天候自動聚焦
  
  利用javascript來轉換
  
  <style>
  BODY {
  FONTSIZE: pt; PADDINGRIGHT: px; PADDINGLEFT: px; PADDINGBOTTOM: px; PADDINGTOP: px;
  }
  input {
  FONTSIZE: pt; height: pt;
  }
  </style>
  
  <script language=JavaScript>
  /*
  This following code are designed and writen by Windy_sk <>
  You can use it freely but u must held all the copyright items!
  */
  
  function StrUnicode(str){
  var arr = new Array();
  for(var i=;i<strlength;i++){
  arr[i]=&# + strcharCodeAt(i) + ;;
  }
  return(arrtoString()replace(//g));
  }
  
  function UnicodeoStr(str){
  var re=/&#[\dafAF]{};/ig;
  var arr=strmatch(re);
  if(arr==null)return();
  for(var i=;i<arrlength;i++){
  arr[i]=StringfromCharCode(arr[i]replace(/[&#;]/g));
  }
  return(arrtoString()replace(//g))
  }
  
  function modi_str(){
  if(dthodchecked){
  if(documentalltextdecodevalue!=){
  documentalltextencodevalue = StrUnicode(documentalltextdecodevalue);
  }else{
  documentalltextdecodevalue = UnicodeoStr(documentalltextencodevalue);
  }
  }else{
  if(documentalltextencodevalue!=){
  documentalltextdecodevalue = UnicodeoStr(documentalltextencodevalue);
  }else{
  documentalltextencodevalue = StrUnicode(documentalltextdecodevalue);
  }
  }
  }
  </script>
  <title>Unicode</title>
  <form name=text>
  文本原型<br>
  <textarea name=decode cols= rows=></textarea>
  <br>
  轉換代碼<br>
  <textarea name=encode cols= rows=></textarea>
  <br>
  <input type=checkbox name=method checked> 正向轉換
  <input type=button onclick=modi_str() value= 確 定 >
  <input type=reset value= 清 空 >
  <input type=button onclick=dthodchecked?documentalltextencodeselect():documentalltextdecodeselect() value= 全 選 >
  </form>
  
  下面是一個顯示所有全角半角的字體的查看例子
  
  <style>
  BODY {
  FONTSIZE: pt; PADDINGRIGHT: px; PADDINGLEFT: px; PADDINGBOTTOM: px; PADDINGTOP: px;
  }
  input {
  FONTSIZE: pt; height: pt;
  }
  </style>
  <script>
  function showUni(minmax){
  showdocumentopen();
  showdocumentwriteln(<style>body{fontsize:pt;wordbreak:breakall;}</style>);
  showdocumentwriteln(min + + max + <br><br>);
  var i=;
  for(i=min;i<=max;i++){
  showdocumentwrite(&# + i + ;);
  }
  showdocumentclose();
  }
  </script>
  <input type=button value=半角 onclick=showUni()>
  <input type=button value=全角 onclick=showUni()>
  <input type=button value=中文 onclick=showUni()>
  <input type=button value=中文 onclick=showUni()>
  <input type=button value=日文平 onclick=showUni()>
  <input type=button value=日文片 onclick=showUni()>
  <input type=button value=韓文 onclick=showUni()>
  <br>自定義<input name=min> <input name=max>
  <input type=button value=察看 onclick=showUni(parseInt(documentallminvalue)parseInt(documentallmaxvalue))>
  <br>
  <iframe src=about:blank id=show width=% height=% scroll=no></iframe>
  
  下面是一個查表(gb)轉換gb到utf的例子 現在有iconv函數這個已經沒有太大的意義了
  
  <?
  function gbutf($gb){
  if(!trim($gb)) return $gb;
  $filename=gbtxt;
  $tmp=file($filename);
  $codetable=array();
  while(list($key$value)=each($tmp))
  $codetable[hexdec(substr($value))]=substr($value);
  
  $utf=;
  while($gb) {
  if (ord(substr($gb))>) {
  $this=substr($gb);
  $gb=substr($gbstrlen($gb));
  $utf=uutf(hexdec($codetable[hexdec(binhex($this))x]));
  }else{
  $this=substr($gb);
  $gb=substr($gbstrlen($gb));
  $utf=uutf($this);
  }
  }
  return $utf;
  }
  
  function uutf($c){
  $str=;
  if ($c < x) {
  $str=$c;
  } else if ($c < x) {
  $str=chr(xC | $c>>);
  $str=chr(x | $c & xF);
  } else if ($c < x) {
  $str=chr(xE | $c>>);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c & xF);
  } else if ($c < x) {
  $str=chr(xF | $c>>);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c>> & xF);
  $str=chr(x | $c & xF);
  }
  return $str;
  }
  
  ?>
From:http://tw.wingwit.com/Article/program/PHP/201311/20820.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.