Code decoded by the encodeURIComponent function in JS using php

Source: Internet
Author: User
In JS, encodeURIComponent is used to encode Chinese characters. in PHP, iconv (UTF-8, gb2312, $ q) is used, gb2312 depends on your actual application. if you still don't understand why do you use php to decode the encodeURIComponent function in the following article ??
Prerequisites: The Chinese characters before encoding may be gbk, gb2312, UTF-8, and so on.

The 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.

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.