PHP implementation JavaScript escape (), unescape () method

Source: Internet
Author: User
Tags foreach format join ord pack return version
Javascript

The most complete phpescape function

function Phpescape ($STR) {
Preg_match_all ("/[\x80-\xff].| [\x01-\x7f]+/", $str, $NEWSTR);
$ar = $newstr [0];
foreach ($ar as $k => $v) {
if (Ord ($ar [$k]) >=127) {
$tmpString =bin2hex (Iconv ("GBK", "ucs-2", $v));
if (!eregi ("WIN", Php_os)) {
$tmpString = substr ($tmpString, 2,2). substr ($tmpString, 0,2);
}
$reString. = "%u". $tmpString;
} else {
$reString. = Rawurlencode ($v);
}
}
return $reString;
}

//this method under certain Linux systems,
//will test first
function for iconv PHP version problem
//cause this function method is not working properly
//before use Phpescape ($str) {
  Preg_match_all ("/[€-ÿ].| [-]+/", $str, $r);
  $ar = $r [0];
  foreach ($ar as $k => $v) {
    if (ord ($v [0]) < 128) {
       $ar [$k] = Rawurlencode ($v);
      echo $ar [$k]. " <BR> ";
   }
    Else {
      $ar [$k] = "%u". Bin2Hex (Iconv ("GBK", "iso-10646-ucs-2 ", $v));    //Here GBK is the encoding format for the target code, please change the actual situation
      echo $ar [$k]." <BR> ";
   }
 }
  return Join ("", $ar);
}

//This method is the improved version above
//First, determine if platform version is win system
//But some special characters cannot be used for this version
function Phpescape ($str) {
    $sublen =strlen ($STR);
    $reString = "";
    for ($i =0; $i < $sublen; $i + +) {
        if Ord ($str [$ I]) >=127 {
            $tmpString =bin2hex (iconv) (" GBK "," Ucs-2 ", substr ($str, $i, 2)));   //Here GBK the encoding format for the target code, please modify the

if (!eregi ("WIN", Php_os)) {
$tmpString =substr ($tmpString, 2,2). substr ($tmpString, 0,2);
}
$reString. = "%u". $tmpString;
$i + +;
} else {
$reString. = "%" Dechex (ord ($str [$i]);
}
}
return $reString;
}


function Unescape ($STR) {
$str = Rawurldecode ($STR);
Preg_match_all ("/%u.{4}|& #x .{4};|& #d +;|.+/u", $str, $r);
$ar = $r [0];
foreach ($ar as $k => $v) {
if (substr ($v, 0,2) = = "%u")
$ar [$k] = Iconv ("UCS-2", "GBK", Pack ("H4", substr ($v,-4));
ElseIf (substr ($v, 0,3) = = "& #x")
$ar [$k] = Iconv ("UCS-2", "GBK", Pack ("H4", substr ($v, 3,-1));
ElseIf (substr ($v, 0,2) = = "&#") {
$ar [$k] = Iconv ("UCS-2", "GBK", Pack ("n", substr ($v, 2,-1));
}
}
return join ("", $ar);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.