検索
ホームページphp教程php手册PHP を使用して gb2312 と Unicode (UTF-8) の間のエンコード変換を実装します。

下面的例子是将 gb2312 转换为 uft-8 这种形式
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)       $n += ord($c[1]) & 0x3f;
      return $n;
    case 3:
      $n = (ord($c[0]) & 0x1f)       $n += (ord($c[1]) & 0x3f)       $n += ord($c[2]) & 0x3f;
      return $n;
    case 4:
      $n = (ord($c[0]) & 0x0f)       $n += (ord($c[1]) & 0x3f)       $n += (ord($c[2]) & 0x3f)       $n += ord($c[3]) & 0x3f;
      return $n;
  }
}
?>

下面的例子是利用php将uft-8这中编码转换为gb2312.

$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          $str.=$c;
    } else if ($c          $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c          $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c          $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})|.{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) == "")
      $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
    elseif(substr($v,0,2) == "") {
echo substr($v,2,-1)."
";
      $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
    }
  }
  return join("",$ar);
}
$str = "TTL全天候自动聚焦";
echo unescape($str); file://out TTL全天候自动聚焦


利用javascript来转换



Unicode

文本原型:

 
 

转换代码:

 
 

  正向转换
 
 
 

下面是一个显示所有全角半角的字体的查看例子


<script><br/>function showUni(min,max){<br/>show.document.open();<br/>show.document.writeln("<style>body{font-size:9pt;word-break:break-all;}");<br/>show.document.writeln(min + " - " + max + "<br><br>");<br/>var i=0;<br/>for(i=min;i<=max;i++){<br/> show.document.write("&#" + i + ";");<br/>}<br/>show.document.close();<br/>}<br/></script>








自定义: -



下面是一个查表(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 $str.=$c;
} else if ($c $str.=chr(0xC0 | $c>>6);
$str.=chr(0x80 | $c & 0x3F);
} else if ($c $str.=chr(0xE0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.= chr(0x80 | $c & 0x3F);
} else if ($c $str.=chr(0xF0 | $c>>18);
$str.=chr(0x80 | $c>>12 & 0x3F);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
return $str;
}
?>



声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。