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);
}