The code _php technique of encodeURIComponent function in JS with PHP decoding

Source: Internet
Author: User
JS encodeuricomponent function to Chinese code, how to use PHP decoding??
Prerequisite: Before the encoding of the Chinese may be gbk,gb2312,utf-8 and so on.
Copy Code code as follows:

UrlDecode ()
Iconv ()


In the JS used encodeuricomponent to encode the Chinese in PHP using Iconv (' UTF-8 ', ' gb2312 ', $q); You can get the string you need, where gb2312 according to your actual application to decide if you do not understand why read the following article

URL encoding conversion, Escape () encodeURI () encodeURIComponent ()

This article describes three functions for URL encoding Escape (), encodeURI (), encodeURIComponent ()

Escape () Method:
The specified string is encoded using the ISO Latin character set. All spaces, punctuation, special characters, and other non-ASCII characters will be converted into%XX-formatted character encodings (XX equals the encoded 16 digits of the character in the character set table). For example, spaces's corresponding encoding is%20. The Unescape method is the opposite. Characters that will not be encoded by this method: @ */+

encodeURI () Method:
Converts the URI string into a string in escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? + '

encodeURIComponent () Method:
Converts the URI string into a string in escape format using the UTF-8 encoding format. This method encodes more characters than encodeURI (), such as/or characters. So if a string contains several parts of the URI, this method cannot be encoded, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )


Therefore, for Chinese strings, you only need to use escape if you do not want to convert the string encoding format into a UTF-8 format (such as when the original page and the target page charset are consistent). If your page is GB2312 or other code, and the page that accepts the parameter is UTF-8 encoded, you should use encodeURI or encodeuricomponent.

Note: The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encodeURI and encodeURIComponent methods should be used for encoding. In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape was in the javascript1.0 version.

summarize the usage:

1, Http/post mode by default, using the "x-www-form-urlencoded" to encode, it and JavaScript inside the role of the encodeURI is the same;
2, after the content of this encoding, to the background, where the characters are UTF-8 encoded format;
3, if your servlet/jsp specified output using Content-type is UTF-8, congratulations, you do not need to these parameters special coding, decoding operations, it must be normal;
4, otherwise you need to solve these parameter, coding operations, such as your page is GBK code, then you need to write:
String Spara = new String (Request.getparamter ("test"). GetBytes ("Iso-8859-1"), "GBK");
The gb2312 code can also be used to encode GBK;
5. If you are working in eclipse, pay attention to the attributes of your servlet and JSP files, the encoding must be the same as set in Content-type, otherwise javac will encode your file incorrectly, at which point the string of the bytecode file itself is wrong , the output is not the same;
6, finally make a summary:
6.1, the servlet/jsp file itself must be encoded and content-type output consistent, otherwise need to add additional coding, decoding steps;
6.2, Http/post mode and encodeURI mode of the encoding are UTF-8;
6.3, the ordinary window.open (...), that is, the Http/get mode, the encoding is the same as the script to run the page code;
6.4, the background decoder must be decoded through the iso-8859-1, and then use your target code to encode;
6.5, Response.setcontenttype ("Text/xml;charset=utf-8");
If there are Chinese characters back to the page, you must add charset=utf-8 words
Request.setcharacterencoding ("UTF-8"); If the page comes through a post and contains Chinese characters, it must be added to this sentence

This statement is placed at the front of the program;

6.6 Please ensure that the project attribute is encoded as "UTF-8" and that the request page is consistent with the Response page encoding (can be backstage) UTF-8. Must guarantee the file

property is consistent with ContentType settings

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.