一月 8th, 2009php还原escape后的内容
此文章来源于后羿之弓,转载请注明出处
转载内容,主要是为了解决经过js的escape后的如’%u5B9D%u9A6C’内容还原成utf-8。有兴趣的可以看一下。
方法一:
- /**
- * Function converts an Javascript escaped string back into a string with specified charset (default is UTF-8).
- * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
- *
- * @param string $source escaped with Javascript's escape() function
- * @param string $iconv_to destination character set will be used as second paramether in the iconv function. Default is UTF-8.
- * @return string
- */
- function unescape($source, $iconv_to = 'UTF-8') {
- $decodedStr = '';
- $pos = 0;
- $len = strlen ($source);
- while ($pos < $len) {
- $charAt = substr ($source, $pos, 1);
- if ($charAt == '%') {
- $pos++;
- $charAt = substr ($source, $pos, 1);
- if ($charAt == 'u') {
- // we got a unicode character
- $pos++;
- $unicodeHexVal = substr ($source, $pos, 4);
- $unicode = hexdec ($unicodeHexVal);
- $decodedStr .= code2utf($unicode);
- $pos += 4;
- }
- else {
- // we have an escaped ascii character
- $hexVal = substr ($source, $pos, 2);
- $decodedStr .= chr (hexdec ($hexVal));
- $pos += 2;
- }
- }
- else {
- $decodedStr .= $charAt;
- $pos++;
- }
- }
- <br/>if ($iconv_to != 'UTF-8') {
- $decodedStr = iconv('UTF-8', $iconv_to, $decodedStr);
- }
- <br/>return $decodedStr;
- }
- /**
- * Function coverts number of utf char into that character.
- * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
- *
- * @param int $num
- * @return utf8char
- */
- function code2utf($num)
- {
- if($num<128)return chr($num);
- if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
- if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
- if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
- return '';
- }
- exit;
方法二(来自神仙居)
avascript有个escape函数,虽然现在已经不建议使用,但还是会碰到许多escape过的字符串需要解码。因为javascript的escape实际上是个unicode编码,要转成utf8或者其他编码是很麻烦的。php5.2内置的json扩展除了用于json以外,其实也可以用来unescape。
json / javascript里的字符串在字符串常量的表示里,也可以用\u5C71这样的方式,而escape的结果里,只是把那个 \ 换成了 % 。所以,只要用类似下面的代码就可以转换回来。而对于\u5C71这种形式的编码的串,只需要在两头加上双引号,然后json_decode就可以了。
- echo json_decode(str_replace('%','\\', "%u5B9D%u9A6C"));