JS in the encodeURIComponent function with PHP decoding code

Source: Internet
Author: User

JS in the encodeURIComponent function to the Chinese code, how to use PHP decoding??
Prerequisite: The Chinese before encoding may be gbk,gb2312,utf-8 and so on.

Copy CodeThe code is as follows:
UrlDecode ()
Iconv ()



In JS using encodeURIComponent to encode Chinese in PHP using Iconv (' UTF-8 ', ' gb2312 ', $q), you can get the string you need, which gb2312 according to your actual application to set as still do not understand why read the following article

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

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

Escape () Method:
Encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+

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

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


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

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

To summarize the usage:

1, Http/post mode in the default case, the use of "x-www-form-urlencoded" encoding, it and JavaScript inside the role of encodeURI is the same;
2, after the content of this encoding, arrived backstage, the characters are UTF-8 encoded format;
3, if your servlet/jsp designated output use Content-type is UTF-8, congratulations, you do not need to these parameters special encoding, decoding operation, it must be normal;
4, otherwise you need to solve these parameter, encoding operations, such as your page is GBK encoding, then you need to write:
String Spara = new String (Request.getparamter ("test"). GetBytes ("Iso-8859-1"), "GBK");
The use of GB2312 encoding can also be GBK encoded;
5, if you are working in Eclipse, please pay attention to the properties of your servlet and JSP files, where the encoding must be the same as set in Content-type, otherwise javac will be wrong for your file encoding, this time the bytecode file itself is the wrong string , the output is not the same;
6, finally do a summary:
6.1, the servlet/jsp file itself must be encoded with the content-type output of the same, otherwise need to add additional encoding, decoding steps;
6.2, the Http/post way and the encodeURI way incoming code are UTF-8;
6.3, the ordinary window.open (...), that is, the Http/get mode, the incoming encoding is consistent with the page encoding that runs the script;
6.4, the background decoding must be decoded through iso-8859-1, and then use your target encoding to encode;
6.5, Response.setcontenttype ("Text/xml;charset=utf-8");
If the Chinese characters echo to the page, add charset=utf-8 words
Request.setcharacterencoding ("UTF-8");//pages such as through post, containing Chinese characters, must add this sentence

This statement is placed at the front of the program;

6.6 Ensure that the project property is encoded as "UTF-8" and that the request page is encoded with the response page (can be in the background) UTF-8. Documents must be guaranteed

Properties and ContentType settings are consistent

JS in the encodeURIComponent function with PHP decoding code

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.