How does the encodeURIComponent function in JS decode Chinese characters using php ??
Prerequisites: The Chinese characters before encoding may be gbk, gb2312, UTF-8, and so on.
Copy codeThe Code is as follows:
Urldecode ()
Iconv ()
EncodeURIComponent is used in JS to encode Chinese characters. in PHP, iconv ('utf-8', 'gb2312 ', $ q) is used to obtain the required strings, gb2312 depends on your actual application. If you still don't understand why, read the following article.
URL encoding conversion, escape () encodeURI () encodeURIComponent ()
This article introduces three functions of url encoding: escape (), encodeURI (), encodeURIComponent ()
Escape () method:
The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The unescape method is the opposite. Characters not encoded by this method: @ */+
EncodeURI () method:
Convert a URI string to an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'
EncodeURIComponent () method:
Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()
Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is GB2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeURI or encodeURIComponent.
Note: The escape method cannot be used to encode the Uniform Resource Identifier (uri. Use the encodeuri and encodeuricomponent methods for encoding. In addition, encodeURI/encodeURIComponent is introduced after javascript1.5, and escape is available in javascript1.0.
To sum up the usage:
1. By default, the HTTP/POST method uses "x-www-form-urlencoded" for encoding. It plays the same role as encodeURI in JavaScript;
2, after such encoding content, to the background, the characters are UTF-8 encoding format;
3. If your Servlet/JSP specifies that the Content-Type used by the output is the UTF-8, congratulations, you don't need to perform special encoding and decoding operations on these Parameters, it must be normal;
4. Otherwise, you need to decode and encode the parameters. For example, if your page is GBK encoded, you need to write the following code:
String sPara = new String (request. getParamter ("test"). getBytes ("iso-8859-1"), "GBK ");
Gb2312 encoding can also be used;
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 that set in content-type, otherwise, javac will incorrectly encode your file. At this time, the character string of the bytecode file itself is incorrect, and the output is not used;
6. Make a summary:
6.1 The servlet/JSP file encoding must be consistent with the content-type output; otherwise, additional encoding and decoding steps must be added;
6.2, HTTP/POST and encodeURI encoding are passed in UTF-8;
6.3. The normal window. open (...), that is, the HTTP/GET method, is passed in encoding consistent with the page code running the script;
6.4, the background decoding must be decoded through the iso-8859-1, and then use your target encoding;
6.5. response. setContentType ("text/xml; charset = UTF-8 ");
If a Chinese character is displayed on the page, the character charset = UTF-8 must be added.
Request. setCharacterEncoding ("UTF-8"); // If the page is POST, containing Chinese characters, you must add this sentence
This statement is placed at the beginning of the program;
6.6 ensure that the project property code is "UTF-8", the request page is consistent with the response page Code (which can be a background) UTF-8. The file must be secured
The attribute is consistent with the ContentType setting.