The processing method of JS and PHP to URL encoding

Source: Internet
Author: User
Tags chr ord strlen

  This article mainly introduces JS and PHP to URL encoding processing methods, the need for friends can refer to the following

Solution: Use JS to the URL of the Chinese characters to escape code.     Code as follows: <a href= "onclick=" (' product_list.php?p_sort= ' +escape (' PHP Development Resource Network ')); " >   After clicking the link:    reference: U7f51   generates such an effect that it is obvious that using PHP's UrlDecode () or Base64_decode () is not solvable.     Solutions, write an inverse function in PHP:      Code as follows: function Js_unescape ($str) {        $ret = ' ';         $len = strlen ($STR);         for ($i = 0; $i < $len; $i + +) {                if ( $STR [$i] = = '% ' && $str [$i +1] = = ' U ') {                    &NBSP ;   $val = Hexdec (substr ($str, $i +2, 4));                       if ($val < 0x7f) $ret. = Chr ($val);                       ELSE if ($val < 0x800) $ret . = Chr (0xc0| ( $val >>6)). Chr (0x80| ( $val &0x3f));                         else $ret. = Chr (0xe0| ( $val >>12)). Chr (0x80| ( ($val >>6) &0x3f)). Chr (0x80| ( $val &0x3f));                         $i + 5;                                   E LSE if ($STR [$i] = = '% ') {                        $ret. = Urld Ecode (substr ($str, $i, 3));                         $i + 2;                                 Else $re T. = $str [$i];        }        return $ret;       Note that the JS code will be automatically converted into UTF-8, so you must encode the conversion to get the correct results, otherwise it will be Chinese garbled. The     code is as follows:Print iconv (' utf-8 ', ' gb2312 ', Js_unescape ($_request[' p_sort '));     To this we have successfully reversed the JS escape code.     In addition, I found a php to implement JS escape code function:    code as follows: function Phpescape ($str) {$sublen =strlen ($STR); Retrunstring= ""; For ($i =0 $i < $sublen $i + +) {if (Ord ($str [$i]) >=127) {$tmpString =bin2hex iconv ("gb2312", "Ucs-2", substr ($STR, $ i,2)));        //$tmpString =substr ($tmpString, 2,2). substr ($tmpString, 0,2); You may want to open this item under window $retrunString . = "%u". $tmpString; $i + +; else {        $retrunString. = "%". Dechex (Ord ($str [$i]));} return $retrunString; }  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.