1 2 function escape($str) {
3 preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
4 $ar = $r[0];
5 foreach($ar as $k=>$v) {
6 if(ord($v[0]) < 128)
7 $ar[$k] = rawurlencode($v);
8 else
9 $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
10 }
11 return join("",$ar);
12 }
13 function unescape($str) {
14 $str = rawurldecode($str);
15 preg_match_all("/(?:%u.{4})|.+/",$str,$r);
16 $ar = $r[0];
17 foreach($ar as $k=>$v) {
18 if(substr($v,0,2) == "%u" && strlen($v) == 6)
19 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
20 }
21 return join("",$ar);
22 }
23
24
25 $a="\u4e2d\u897f\u533a";
26 $a=str_replace('\\','%',$a);
27 echo unescape($a);
28 ?>
没有评论:
发表评论