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) $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將"全"這中編碼轉換為gb2312. 下面的例子是利用php將"全"這中編碼轉換為gb2312.
複製程式碼 程式碼如下:
$str = "TTL全天候自動聚焦";
$str = "TTL全天候自動聚焦";
$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); / /out TTL全天候自動聚焦
利用javascript來轉換複製程式碼
程式碼如下:
正文{
字體大小:9pt;右內邊距:0px;左內邊距:0px;底部填充:0px;頂部填充:0px;
}
輸入{
字體大小:9pt;高度:13pt;
}
樣式>
/*
以下程式碼由Windy_sk
設計和編寫
您可以自由使用,但您必須擁有所有版權!
*/
函數Str2Unicode(str){
var arr = new Array();
for(var i=0;iarr[i ]="" str.charCodeAt(i) ";";
}
return(arr.toString().replace(/,/g,""));
}
函數Unicode2oStr(str){
var re=/[da-fA-F]{1,5};/ig;
var arr=str.match(re);
if(arr ==null)return("");
for(var i=0;iarr[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);
}
}
}
>}
}
}
腳本>
Unicode標題>;
;
文字原型:
轉換代碼:
正向轉換
表格>
下面是一個顯示所有全角半角字體的檢視範例
複製程式碼
程式碼如下:
正文{
字型大小:9pt;右內邊距:0px;左內邊邊邊距:0px;底部填充:0px;頂部填充:0px;
}
輸入{
字體大小:9pt;高度:13pt;
}
樣式>
function showUni(min,max){
show.document.open();
show.document.writeln("");
show.document.writeln(min " - " max "
");
var i=0;
for(i= min;ishow.document.write("" i ";");
}
show.document.close();
}
腳本>
自訂: -
>
下面是一個查表(gb2312),轉換gb2312到utf8的例子,現在有iconv函數,這個已經沒有了繁瑣的意義了,
複製程式碼程式碼如下:
函數gb2utf8($gb){
if(!trim($gb)) return $gb;
$filename="gb2312.txt";
$ tmp=檔($檔名);
$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]));
}其他{
$this=substr($gb,0,1);
$🎜>$this=substr($gb,0,1);
$ gb=substr($gb,1,strlen($gb)-1);
$utf8.=u2utf8($this);
}
}
回傳$utf8;
}
}
回傳$utf8;
}
函數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);
}
回傳$str; } ? >
http://www.bkjia.com/PHPjc/313788.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313788.html
TechArticlegb2312 和unicode 間編碼的轉換下面的例子將gb2312 轉換為「」這種形式以後php4.3.1 以後phpv函數很好用的,只需要自己寫一個uft8到unicode的轉...