gb
和 unicode 間的編碼轉換
下面的例子是將 gb
轉換為
;
這種形式
php
以後的iconv函數很好用的
只是需要自己寫一個uft
到unicode的轉換函數
查表(gb
txt)也行
<?
$text =
電子書庫
;
preg_match_all(
/[\x
\xff]?
/
$text
$ar);
foreach($ar[
] as $v)
echo
utf
_unicode(iconv(
GB
UTF
$v))
;
;
?>
<?
// utf
> unicode
function utf
_unicode($c) {
switch(strlen($c)) {
case
:
return ord($c);
case
:
$n = (ord($c[
]) &
x
f) <<
;
$n += ord($c[
]) &
x
f;
return $n;
case
:
$n = (ord($c[
]) &
x
f) <<
;
$n += (ord($c[
]) &
x
f) <<
;
$n += ord($c[
]) &
x
f;
return $n;
case
:
$n = (ord($c[
]) &
x
f) <<
;
$n += (ord($c[
]) &
x
f) <<
;
$n += (ord($c[
]) &
x
f) <<
;
$n += ord($c[
]) &
x
f;
return $n;
}
}
?>
下面的例子是利用php將
;
這中編碼轉換為gb
<?php
$str =
TTL
;
;
;
;
;
;
;
;
$str = preg_replace(
|([
]{
});|
\
u
utf
gb(\\
)
\
$str);
$str =
\$str=\
$str\
;
;
eval($str);
echo $str;
function u
utf
gb($c){
$str=
;
if ($c <
x
) {
$str
=$c;
} else if ($c <
x
) {
$str
=chr(
xC
| $c>>
);
$str
=chr(
x
| $c &
x
F);
} else if ($c <
x
) {
$str
=chr(
xE
| $c>>
);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c &
x
F);
} else if ($c <
x
) {
$str
=chr(
xF
| $c>>
);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c &
x
F);
}
return iconv(
UTF
GB
$str);
}
?>
或者是
function unescape($str) {
$str = rawurldecode($str);
preg_match_all(
/(?:%u
{
})|
{
};|\d+;|
+/U
$str
$r);
$ar = $r[
];
print_r($ar);
foreach($ar as $k=>$v) {
if(substr($v
) ==
%u
)
$ar[$k] = iconv(
UCS
GB
pack(
H
substr($v
)));
elseif(substr($v
) ==
)
$ar[$k] = iconv(
UCS
GB
pack(
H
substr($v
)));
elseif(substr($v
) ==
) {
echo substr($v
)
<br>
;
$ar[$k] = iconv(
UCS
GB
pack(
n
substr($v
)));
}
}
return join(
$ar);
}
$str =
TTL
;
;
;
;
;
;
;
;
echo unescape($str); //out TTL全天候自動聚焦
利用javascript來轉換
<style>
BODY {
FONT
SIZE:
pt; PADDING
RIGHT:
px; PADDING
LEFT:
px; PADDING
BOTTOM:
px; PADDING
TOP:
px;
}
input {
FONT
SIZE:
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 Str
Unicode(str){
var arr = new Array();
for(var i=
;i<str
length;i++){
arr[i]=
+ str
charCodeAt(i) +
;
;
}
return(arr
toString()
replace(/
/g
));
}
function Unicode
oStr(str){
var re=/[\da
fA
F]{
};/ig;
var arr=str
match(re);
if(arr==null)return(
);
for(var i=
;i<arr
length;i++){
arr[i]=String
fromCharCode(arr[i]
replace(/[]/g
));
}
return(arr
toString()
replace(/
/g
))
}
function modi_str(){
if(dthod
checked){
if(document
all
text
decode
value!=
){
document
all
text
encode
value = Str
Unicode(document
all
text
decode
value);
}else{
document
all
text
decode
value = Unicode
oStr(document
all
text
encode
value);
}
}else{
if(document
all
text
encode
value!=
){
document
all
text
decode
value = Unicode
oStr(document
all
text
encode
value);
}else{
document
all
text
encode
value = Str
Unicode(document
all
text
decode
value);
}
}
}
</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=
dthod
checked?document
all
text
encode
select():document
all
text
decode
select()
value=
全 選
>
</form>
下面是一個顯示所有全角半角的字體的查看例子
<style>
BODY {
FONT
SIZE:
pt; PADDING
RIGHT:
px; PADDING
LEFT:
px; PADDING
BOTTOM:
px; PADDING
TOP:
px;
}
input {
FONT
SIZE:
pt; height:
pt;
}
</style>
<script>
function showUni(min
max){
show
document
open();
show
document
writeln(
<style>body{font
size:
pt;word
break:break
all;}</style>
);
show
document
writeln(min +
+ max +
<br><br>
);
var i=
;
for(i=min;i<=max;i++){
show
document
write(
+ i +
;
);
}
show
document
close();
}
</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(document
all
min
value)
parseInt(document
all
max
value))>
<br>
<iframe src=
about:blank
id=show width=
% height=
% scroll=no></iframe>
下面是一個查表(gb
)
轉換gb
到utf
的例子
現在有iconv函數
這個已經沒有太大的意義了
<?
function gb
utf
($gb){
if(!trim($gb)) return $gb;
$filename=
gb
txt
;
$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($gb
strlen($gb)
);
$utf
=u
utf
(hexdec($codetable[hexdec(bin
hex($this))
x
]));
}else{
$this=substr($gb
);
$gb=substr($gb
strlen($gb)
);
$utf
=u
utf
($this);
}
}
return $utf
;
}
function u
utf
($c){
$str=
;
if ($c <
x
) {
$str
=$c;
} else if ($c <
x
) {
$str
=chr(
xC
| $c>>
);
$str
=chr(
x
| $c &
x
F);
} else if ($c <
x
) {
$str
=chr(
xE
| $c>>
);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c &
x
F);
} else if ($c <
x
) {
$str
=chr(
xF
| $c>>
);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c>>
&
x
F);
$str
=chr(
x
| $c &
x
F);
}
return $str;
}
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/20820.html