php函数 escape,unescape,unicodeToUtf8....请大家测试。

utf-8和unicode互转的算法的php实现。以及php的escape函数
---------------------------------------------------------------

>6); $str.=chr(0x80 ¦ $c & 0x3F); } else if ($c < 0x10000) { $str.=chr(0xE0 ¦ $c>>12); $str.=chr(0x80 ¦ $c>>6 & 0x3F); $str.=chr(0x80 ¦ $c & 0x3F); } else if ($c < 0x200000) { $str.=chr(0xF0 ¦ $c>>18); $str.=chr(0x80 ¦ $c>>12 & 0x3F); $str.=chr(0x80 ¦ $c>>6 & 0x3F); $str.=chr(0x80 ¦ $c & 0x3F); } return $str; } ?> ``` /** * php版的javascript同名函数 **/ if(! function_exists("unescape")): function unescape($str) { $str = rawurldecode($str); preg_match_all("/%u.{4} ¦&#x.{4}; ¦&#\d+; ¦&#\d+? ¦.+/U",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(substr($v,0,2) == "%u") $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4))); elseif(substr($v,0,3) == "&#x") $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,3,-1))); elseif(substr($v,0,2) == "&#") { $ar[$k] = iconv("UCS-2","GBK",pack("n",preg_replace("/[^\d]/","",$v))); } } return join("",$ar); } endif; /** * php版的javascript同名函数 **/ if(! function_exists("escape")): function escape($str) { preg_match_all("/[\x80-\xff]. ¦[\x01-\x7f]+/",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(ord($v[0]) < 128) $ar[$k] = rawurlencode($v); else $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v)); } return join("",$ar); } endif; ```
Published At
Categories with Web编程
comments powered by Disqus