宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应

[PHP]PHP的escape函数和unescape函数源代码

2016-10-13 00:16:48 点击量:16 标签: 收藏本文

[PHP]PHP的escape函数和unescape函数源代码

  1. //编码,编码后为小写 
  2. function escape($str){ 
  3. preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr); 
  4. $ar = $newstr[0]; 
  5. foreach($ar as $k=>$v){ 
  6.    if(ord($ar[$k])>=127){ 
  7.     $tmpString=bin2hex(iconv("GBK","ucs-2//IGNORE",$v)); 
  8.     if (!eregi("WIN",PHP_OS)){ 
  9.      $tmpString = substr($tmpString,2,2).substr($tmpString,0,2); 
  10.     } 
  11.     $reString.="%u".$tmpString; 
  12.    }else{ 
  13.     $reString.= rawurlencode($v); 
  14.    } 
  15. } 
  16. return $reString; 
  17. } 
  18.  
  19. //解码为HTML实体字符 
  20. function unescape ($source){ 
  21. $decodedStr = ""; 
  22. $pos = 0; 
  23. $len = strlen ($source); 
  24. while ($pos < $len){ 
  25.    $charAt = substr ($source, $pos, 1); 
  26.          if ($charAt == '%'){ 
  27.     $pos++; 
  28.     $charAt = substr ($source, $pos, 1); 
  29.              if ($charAt == 'u'){ 
  30.      // we got a unicode character 
  31.      $pos++; 
  32.      $unicodeHexVal = substr ($source, $pos, 4); 
  33.      $unicode = hexdec ($unicodeHexVal); 
  34.      $entity = "&#". $unicode . ';'; 
  35.      $decodedStr .= utf8_encode ($entity); 
  36.      $pos += 4; 
  37.              }else{ 
  38.      // we have an escaped ascii character 
  39.      $hexVal = substr ($source, $pos, 2); 
  40.      $decodedStr .= chr (hexdec ($hexVal)); 
  41.      $pos += 2; 
  42.     } 
  43.    }else{ 
  44.     $decodedStr .= $charAt; 
  45.     $pos++; 
  46.    } 
  47. } 
  48. return $decodedStr; 
  49. } 
  50.  
  51.  
  52. //直接解码为字符串。网上找到的这个版本的函数是解码为HTML实体字符,这是我修改的 
  53. function unescape($source){ 
  54. $decodedStr = ""; 
  55. $pos = 0; 
  56. $len = strlen ($source); 
  57. while ($pos < $len){ 
  58.    $charAt = substr ($source, $pos, 1); 
  59.          if ($charAt == '%'){ 
  60.     $pos++; 
  61.     $charAt = substr ($source, $pos, 1); 
  62.              if ($charAt == 'u'){ 
  63.      // we got a unicode character 
  64.      $pos++; 
  65.      $unicodeHexVal = substr ($source, $pos, 4); 
  66.      $unicode = hexdec ($unicodeHexVal); 
  67.      $decodedStr .= u2utf82gb($unicode); 
  68.      $pos += 4; 
  69.              }else{ 
  70.      // we have an escaped ascii character 
  71.      $hexVal = substr ($source, $pos, 2); 
  72.      $decodedStr .= chr (hexdec ($hexVal)); 
  73.      $pos += 2; 
  74.     } 
  75.    }else{ 
  76.     $decodedStr .= $charAt; 
  77.     $pos++; 
  78.    } 
  79. } 
  80. return $decodedStr; 
  81. } 
  82. function u2utf82gb($c){ 
  83. $strphp = ""; 
  84. if($c < 0x80){ 
  85.    $strphp .= $c; 
  86. }elseif($c < 0x800){ 
  87.    $strphp .= chr(0xC0 | $c>>6); 
  88.    $strphp .= chr(0x80 | $c & 0x3F); 
  89. }elseif($c < 0x10000){ 
  90.    $strphp .= chr(0xE0 | $c>>12); 
  91.    $strphp .= chr(0x80 | $c>>6 & 0x3F); 
  92.    $strphp .= chr(0x80 | $c & 0x3F); 
  93. }elseif($c < 0x200000){ 
  94.    $strphp .= chr(0xF0 | $c>>18); 
  95.    $strphp .= chr(0x80 | $c>>12 & 0x3F); 
  96.    $strphp .= chr(0x80 | $c>>6 & 0x3F); 
  97.    $strphp .= chr(0x80 | $c & 0x3F); 
  98. } 
  99. return iconv('UTF-8', 'GB2312', $strphp); 
  100. }