How to deal with url encoding in js and php

Source: Internet
Author: User

This article mainly introduces how to deal with url encoding in js and php. For more information, see

Solution: Use js to encode the Chinese characters in the URL. The Code is as follows: <a href = "" onclick = "window. open ('product _ list. php? P_sort = '+ escape ('php development resource'); "> when you click the link, reference: http: // 127.0.0.1/shop/product_list.php? P_sort = PHP % u5F00 % u53D1 % u8D44 % u6E90 % u7F51 generated this effect. Obviously, using PHP urldecode () or base64_decode () cannot be reversed. Solution: Use PHP to write an anti-decoding function. The Code is as follows: function js_unescape ($ str) {$ ret = ''; $ len = strlen ($ str ); for ($ I = 0; $ I <$ len; $ I ++) {if ($ str [$ I] = '%' & $ str [$ I + 1] = 'U ') {$ 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 ;} Else if ($ str [$ I] = '%') {$ ret. = urldecode (substr ($ str, $ I, 3); $ I + = 2;} else $ ret. = $ str [$ I];} return $ ret;} note that JavaScript code is automatically converted to a UTF-8, so the Code must be converted to get the correct results, otherwise the Chinese garbled. The Code is as follows: print iconv ('utf-8', 'gb2312', js_unescape ($ _ REQUEST ['P _ sort ']); at this point, we have successfully reversed the js escape code. In addition, I found a function using PHP to implement js escape encoding: the code is 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,); this option may be enabled in window $ retrunString. = "% u ". $ tmpString; $ I ++;} else {$ retrunString. = "% ". dechex (ord ($ str [$ I]) ;}return $ retrunString ;}

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.