This article describes the PHP custom function implementation of JS escape method. Share to everyone for your reference, as follows:
PHP functionfunction 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). substr ($charVal, 0, 2); $output. = '%u '. $charVal; } else {if (false!== Strpos ($special, $SINGLESTR)) $output. = $singleStr; else $output. = "%". Dechex (Ord ($string [$bn]); } $bn = $n; } return $output;}
For more information about PHP related content readers can view the topic: "PHP coding and transcoding Operation skills Summary", "PHP String Usage Summary", "PHP array" operation skill Daquan, "PHP Sorting algorithm Summary", " PHP common traversal algorithm and skills summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Math Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator usage Summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
The above introduces the JS function PHP custom function to implement JS's Escape method example, including the JS function aspects, I hope that the PHP tutorial interested in a friend helpful.