-
- function unescape($str) {
- $str = rawurldecode($str);
- preg_match_all("/(?:%u.{4})|.+/",$str ,$r);
- $ar = $r[0];
- foreach($ar as $k=>$v) {
- if(substr($v,0,2) == '%u' && strlen ($v) == 6)
- $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,-4)));
- }
- return join("",$ar);
- }
-
Copying the code
has a little problem, so I changed it to another function, which seems to be more powerful.
-
- function unescape($str) {
- $str = rawurldecode($str);
- preg_match_all("/%u.{4}|.{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","utf-8",pack("H4",substr($v,- 4)));
- elseif(substr($v,0,3) == "")
- $ar[$k] = iconv("UCS-2","utf-8",pack(" H4",substr($v,3,-1)));
- elseif(substr($v,0,2) == "") {
- $ar[$k] = iconv("UCS-2 ","utf-8",pack("n",preg_replace("/[^d]/","",$v)));
- }
- }
- return join("",$ar);
- }
-
Copy the code
After using it for a while, I found that it can be used locally, but not in our online environment.
Online is *nux, local is XP, and the PHP version is different.
Later, I found a similar function in the manual
It also supports utf8. I personally think it should be more versatile.
-
-
- //php character transcoding
- function utf8RawUrlDecode ($source) {
- $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);
- $entity = "". $unicode . ';';
- $decodedStr .= utf8_encode ($entity);
- $pos += 4;
- }
- else {
- // we have an escaped ascii character
- $hexVal = substr ($source, $pos, 2);
- $decodedStr .= chr (hexdec ($hexVal));
- $pos += 2;
- }
- } else {
- $decodedStr .= $ charAt;
- $pos++;
- }
- }
- return $decodedStr;
- }
-
Copy code
Use this function to successfully solve the problem.
|