This example describes the PHP custom function to implement JS escape method. Share to everyone for your reference, specific as follows:
php function function Escape ($string) {$n = $bn = $tn = 0;
$output = ';
$special = "-_.+@/*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
while ($n < strlen ($string)) {$ascii = Ord ($string [$n]); if ($ascii = = 9 | | $ascii = 10 | | (<= $ascii && $ascii <= 126))
{$tn = 1; $n + +;
} elseif (194 <= $ascii && $ascii <= 223) {$tn = 2; $n + = 2;
} elseif (224 <= $ascii && $ascii <= 239) {$tn = 3; $n + = 3;
} elseif (<= $ascii && $ascii <= 247) {$tn = 4; $n + = 4;
} elseif (248 <= $ascii && $ascii <= 251) {$tn = 5; $n + = 5;
} elseif ($ascii = = 252 | | $ascii = = 253) {$tn = 6; $n + = 6;
else {$n + +;
} $singleStr = substr ($string, $bn, $tn);
$charVal = Bin2Hex (Iconv (' utf-8 ', ' ucs-2 ', $singleStr)); if (Base_convert ($cHarval > 0xFF) {if (!preg_match ("/win/i", php_os)) $charVal = substr ($charVal, 2, 2). s
Ubstr ($charVal, 0, 2); $output. = '%u '.
$charVal;
else {if (false!== Strpos ($special, $SINGLESTR)) $output. = $singleStr; else $output. = "%".
Dechex (Ord ($string [$bn]));
} $bn = $n;
return $output;
}
More about PHP Interested readers can view the site topics: "PHP code and transcoding Operation Tips Summary", "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP Sorting algorithm Summary", " PHP commonly used traversal algorithm and skills summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator usage Summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.