All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
phpMyAdmin2.6以上版本数据乱码问题_[PHP教程]
ASP.NET,2.0,中的窗体身份验证_[Asp.Net教程]
包含实时功能的ASP.NET系统结构_.net资料_编程技术
在系统中生成Excel流并传给用户_[Asp.Net教程]
PHP环境下配置在线编辑器FCKeditor_php资料_编程技术
PHP防注入攻击过滤HTML代码函数_[PHP教程]
在SQL,Server,2005中实现表的行列转换_[SQL,Server教程]
网页设计中HTML常犯的五个错误_[Html教程]
基于PHP的AJAX技术实现文件异步上传_[PHP教程]
对PHP程序中的常见漏洞进行攻击_[PHP教程]
Reading number is top 10 pictures
A man's favorite things4
From China fortress sora aoi2
鸡蛋的新玩法
Small QiShu -- ShuangShuangPan2
到底是谁撞谁呀?
Chinese paper-cut grilles art appreciation7
Startling Russian girl blind date scene2
2012 national geographic daily picture1
如果没有好报,为什么要做好人?
Photographed the passion of the clients and prostitutes in the sex trade picture2
Download software ranking
Red cliff
Tram sex maniac 2 (H) rar bag11
实战黑客不求人
VeryCD电驴(EasyMule) V1.1.9 Build09081
Sora aoi‘s film--Lust fan wall
Love the forty days
Call Of Duty2
C#编程思想
美女写真3
jdk1.5
aaa published in(发表于) 2013/12/21 22:40:55 Edit(编辑)
用php实现gb2312和unicode(UTF-8)间的编码转换_php资料_编程技术

用php实现gb2312和unicode(UTF-8)间的编码转换_php资料_编程技术

用php实现gb2312和unicode(UTF-8)间的编码转换_php资料_编程技术-你的首页-uuhomepage.com







下面的例子是将 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) << 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将uft-8这中编码转换为gb2312.


$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)."
";
$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); file://out TTL全天候自动聚焦



利用javascript来转换




Unicode

文本原型:




转换代码:




正向转换






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












自定义: -






下面是一个查表(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 < 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 $str;
}
?>























添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.