qswhdata=file($filename); } function decode($str,$pattern=0){ $arr=array(/(/w+);/iu,/((%/w/w)+)/i,/%u(/w{4,5})/iu); if(is_integer($pattern)){ if($pattern>=count($arr))die(invalid function); $pattern=$arr[$pattern]; } return preg_replace_callback($pattern,array($this,u2gb),$str); } // 程序员之家 bbs.it-home.org function u2gb($arr){ /******(qiushuiwuhen 2002-8-15)******/ $ret=;$str=$arr[1]; if(preg_match_all(/%/w{2}/,$str,$matches)){ for($i=0;$i $chr1=hexdec(substr($matches[0][$i],1)); $arr=array(f0,e0,c0,0); for($j=0;$jhexdec($arr[$j]))break; $chr=hexdec(substr($matches[0][$i],1))-hexdec($arr[$j]); while(++$j $str=dechex($chr); if(strlen($str)==4){ $p=hexdec(substr($str,0,2))-0x4d; $q=hexdec(substr($str,2))*4; $ret.=chr(hexdec(substr($this->qswhdata[$p],$q,2))); $ret.=chr(hexdec(substr($this->qswhdata[$p],$q+2,2))); }else $ret.=chr(hexdec($str)); } } else{ if(strtolower($str[0])==x) $str=substr($str,1); else if(strlen($str)!=4)$str=dechex($str); if(strlen($str)==4){ $p=hexdec(substr($str,0,2))-0x4d; $q=hexdec(substr($str,2))*4; $ret.=chr(hexdec(substr($this->qswhdata[$p],$q,2))); $ret.=chr(hexdec(substr($this->qswhdata[$p],$q+2,2))); }else $ret.=chr(hexdec($str)); } return $ret; } }
复制代码
使用实例:
$qswh=new qswhu(qswhu.php);//如果文件名是qswhu.php,可省参数echo 不带参数(默认过滤为:[num];):;echo /n.$qswh->decode(中文abc);echo /n.$qswh->decode(中文abc);echo /n调用内置过滤(utf转码):.$qswh->decode(%e4%b8%ad%e6%96%87%20!%22%23%24%25%26'()*%2b%2c%2f%3a%3b%3c%3d%3e%3f%40%5b%5d%5e%60%7b%7c%7d~%25abc,1);echo /n调用内置过滤unescape(%u[num]):.$qswh->decode(%u4e2d%u6587abc,2);echo /n自定义过滤([x+num]):.$qswh->decode([x4e2d][x6587][x41][x62][x63],//[(/w+)/]/);
复制代码
效果如下:不带参数(默认过滤为:[num];):中文abc中文abc调用内置过滤(utf转码):中文 !#$%&'()*+,/:;?@[]^`{|}~%abc调用内置过滤unescape(%u[num]):中文abc自定义过滤([x+num]):中文abc
