用php来解析javascript函数escape编码过的字符串
PHP和Javascript都要HTML encoding的函数,但有区别,如下:
- PHP:
- urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
- urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
- Javascript:
- escape(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding
事例:
- $str = uniDecode($str,'big-5');
- function uniDecode($str,$charcode){
- $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);
- return mb_convert_encoding($text, $charcode, 'utf-8');
- }
- function toUtf8($ar){
- foreach($ar as $val){
- $val = intval(substr($val,2),16);
- if($val < 0x7F){ // 0000-007F
- $c .= chr($val);
- }elseif($val < 0x800) { // 0080-0800
- $c .= chr(0xC0 | ($val / 64));
- $c .= chr(0x80 | ($val % 64));
- }else{ // 0800-FFFF
- $c .= chr(0xE0 | (($val / 64) / 64));
- $c .= chr(0x80 | (($val / 64) % 64));
- $c .= chr(0x80 | ($val % 64));
- }
- }
- return $c;
- }