Home >php教程 >php手册 >php版的escape函数

php版的escape函数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 19:40:151616browse

php提供的URL编码 函数 是基于字节的,对由ie的javascript 函数 escape编码的数据就无能为力了。 因此在此共享本人的php版的escape/unescape 函数 function escape($str) { preg_match_all("/[/x80-/xff].|[/x01-/x7f]+/",$str,$r); $ar = $r[0]; foreach($a

php提供的URL编码函数是基于字节的,对由ie的javascript函数escape编码的数据就无能为力了。
因此在此共享本人的php版的escape/unescape函数

function escape($str) {
  preg_match_all("/[/x80-/xff].|[/x01-/x7f]+/",$str,$r);
  $ar = $r[0];
  foreach($ar as $k=>$v) {
    if(ord($v[0])       $ar[$k] = rawurlencode($v);
    else
      $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
  }
  return join("",$ar);
}

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","GB2312",pack("H4",substr($v,-4)));
  }
  return join("",$ar);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn