文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>php的汉字转换:GBK->Unicode(UTF8)

php的汉字转换:GBK->Unicode(UTF8)

时间:2007-02-17  来源:PHP爱好者

php的汉字转换一直是比较麻烦的事www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T 该类内置了四个函数"htmlHex","htmlDec","escape","u2utf8"
方便用户的使用,同时也可自定义函数进行自己喜欢的操作
China It Power . Comi9FOYZYDJqEvAbCNpldjEVf0T
qswhGBK.php 从这里下载
http://www.blueidea.com/user/qswh/qswhGBK.zipwww.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T <?
class qswhGBK{
var $qswhData;
function qswhGBK($filename="qswhGBK.php"){
$this->qswhData=file($filename);
}
function gb2u($gb,$callback=""){
/******(qiushuiwuhen 2002-8-15)******/
$ret="";
for($i=0;$i<strlen($gb);$i++){
if(($p=ord(substr($gb,$i,1)))>127){

$q=ord(substr($gb,++$i,1));
$q=($q-($q>128?65:64))*4;
$q=substr($this->qswhData[$p-128],$q,4);
}
else
$q=dechex($p);
if(empty($callback))
$ret.=$q;
else {
$arr=array("htmlHex","htmlDec","escape","u2utf8");
if(is_integer($callback)){
if($callback>count($arr))die("Invalid Function");
$ret.=$this->$arr[$callback-1]($q);
}else
$ret.=$callback($q);
}
}
return $ret;
}

function htmlHex($str){
return "&#x".$str.";";
}

function htmlDec($str){
return "&#".hexdec($str).";";
}

function escape($str){
return hexdec($str)<256?chr(hexdec($str)):"%u".$str;
}

function u2utf8($str){
/******(qiushuiwuhen 2002-8-15)******/
$sp="!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
$dec=hexdec($str);
$bin=decbin($dec);
$len=strlen($bin);
$arr=array("c0","e0","f0");
if($dec>0x7f){
$ret="";
for($i=$len,$j=-1;$i>=0;$i-=6,$j++){
if($i>6)
$ret="%".dechex(0x80+bindec(substr($bin,$i-6,6))).$ret;
else
$ret="%".dechex(hexdec($arr[$j])+bindec(substr($bin,0,6-$i))).$ret;
}
}else{
if(strpos($sp,chr($dec)))
$ret=chr($dec);
else
$ret="%".strtolower($str);
}
return $ret;
}
}www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T 使用范例
$words="中文Abc";
function ex($str){return "[".$str."]";}www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T
$qswh=new qswhGBK("qswhGBK.php");//如果文件名是qswhGBK.php,可省参数www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T echo("<xmp>不带参数:".$qswh->gb2u($words));
echo("n调用内置函数htmlHex:".$qswh->gb2u($words,1));
echo("n调用内置函数htmlDec:".$qswh->gb2u($words,2));
echo("n调用内置函数escape:".$qswh->gb2u($words,3));
echo("n调用内置函数u2utf8:".$qswh->gb2u($words,4));
echo("n调用自定义函数:".$qswh->gb2u($words,ex));
China It Power . Comi9FOYZYDJqEvAbCNpldjEVf0T
效果如下:www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T不带参数:4E2D6587416263 调用内置函数htmlHex:中文Abc 调用内置函数htmlDec:中文Abc 调用内置函数escape:%u4E2D%u6587Abc 调用内置函数u2utf8:%e4%b8%ad%e6%96%87Abc 调用自定义函数:[4E2D][6587][41][62][63] ps.该文章现仅限在csdn文档中心,如要转载,请和作者联系,否则后果自负。www.c hinaitpower.comli9FOYZYDJqEvAbCNpldjEVf0T
php爱好者站 http://www.phpfans.net Linux|Apache|IIS.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载