-
- //php汉字转码 GBK->Unicode(UTF8)
- class qswhGBK{
- var $qswhData;
- function qswhGBK($filename="qswhGBK.php"){
- $this- >qswhData=file($filename);
- }
- function gb2u($gb,$callback=""){
- /******(秋水烏編 2002-8-15)******/
- $ret="";
- for($i=0; $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("無効な関数");
- $ret.=$ this->$arr[$callback-1]($q);
- }else
- $ret.=$callback($q);
- }
- }
- return $ret;
- }
-
- function htmlHex($str ){
- return "".$str.";";
- } // 程序员之家 bbs.it-home.org
-
- function htmlDec($str){
- return "".hexdec( $str).";";
- }
-
- 関数エスケープ($str){
- return hexdec($str) }
-
- 関数 u2utf8($str){
- /******(秋水烏編 2002-8-15)******/
- $sp="!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
- $dec=hexdec($str);
- $bin=decbin($ dec);
- $len=strlen($bin);
- $arr=array("c0","e0","f0");
- if($dec>0x7f){
- $ret="";
- の($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="% ".strto lower($str);
- }
- return $ret;
- }
- }
复制代
调用例子:
-
-
$words="中文Abc";
- function ex($str){return "[".$str."]";}
$qswh=new qswhGBK("qswhGBK.php");//如果文件名はqswhGBK.php,可省パラメータ
echo(" 不带パラメータ:".$ 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));
-
复制代
效果:
パラメータなし:4E2D6587416263
调用内置関数数htmlHex:中文Abc
调用内置関数数htmlDec:中文Abc
调用内置関数数escape:%u4E2D%u6587Abc
调用内置関数数u2utf8:%e4%b8%ad%e6%96%87Abc
调用自定関数数:[4E2D][6587][41][62][63] |