用php实现gb2312和unicode间的编码转换

2008-02-23 06:26:22来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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将"&#20840;"这中编码转换为gb2312.
<?php
$str = "TTL&#20840;&#22825;&#20505;&#33258;&#21160;&#32858;&#28966;";
$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&#20840;&#22825;&#20505;&#33258;&#21160;&#32858;&#28966;";
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 onclick="modi_str()" value=" 确 定 ">
<input type=reset value=" 清 空 ">
<input type=button onclick="document.all.text.method.checked?document.all.text.encode.select():document.all.text.decode.select()" value=" 全 选 ">
</form>


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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用php发送带附件的Email

下一篇:解决RHAS3中Apache2的PHP上传文件大小的限制