GB2312转换unicode的函数php
时间:2007-02-17 来源:PHP爱好者
// Program by sadly (www.phpx.com)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
function gb2unicode($gb)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
if(!trim($gb))
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
return $gb;
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$filename="gb2312.txt";
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$tmp=file($filename);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$codetable=array();
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
while(list($key,$value)=each($tmp))
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$codetable[hexdec(substr($value,0,6))]=substr($value,9,4);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf="";
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
while($gb)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
if (ord(substr($gb,0,1))>127)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$this=substr($gb,0,2);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$gb=substr($gb,2,strlen($gb));
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf.="&#x".$codetable[hexdec(bin2hex($this))-0x8080].";";
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
else
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$gb=substr($gb,1,strlen($gb));
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf.=substr($gb,0,1);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
return $utf;
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
?>
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
这个函数需要一个gb2312的码表文件,这里下载
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/GB/GB2312.TXT
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
请注意文件名大小写的问题。
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
php爱好者站 http://www.phpfans.net 网页特效|网页模板
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
function gb2unicode($gb)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
if(!trim($gb))
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
return $gb;
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$filename="gb2312.txt";
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$tmp=file($filename);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$codetable=array();
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
while(list($key,$value)=each($tmp))
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$codetable[hexdec(substr($value,0,6))]=substr($value,9,4);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf="";
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
while($gb)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
if (ord(substr($gb,0,1))>127)
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$this=substr($gb,0,2);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$gb=substr($gb,2,strlen($gb));
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf.="&#x".$codetable[hexdec(bin2hex($this))-0x8080].";";
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
else
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
{
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$gb=substr($gb,1,strlen($gb));
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
$utf.=substr($gb,0,1);
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
return $utf;
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
}
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
?>
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
这个函数需要一个gb2312的码表文件,这里下载
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/GB/GB2312.TXT
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
请注意文件名大小写的问题。
chin a i t p oe er . co m06ya2u5uLaZEJ0XiuLPiK3aZu
php爱好者站 http://www.phpfans.net 网页特效|网页模板
相关阅读 更多 +