Home  >  Article  >  Backend Development  >  Code decoded by php using encodeURIComponent function in JS_PHP tutorial

Code decoded by php using encodeURIComponent function in JS_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:361617browse

After the encodeURIComponent function in JS encodes Chinese, how to decode it with php? ?
Premise: The Chinese before encoding may be gbk, gb2312, utf-8, etc.

Copy code The code is as follows:

urldecode()
iconv()


Use encodeURIComponent in JS to encode Chinese. Use iconv('UTF-8', 'gb2312', $q); in PHP to get the string you need. Among them, gb2312 is based on your actual application. Ding Ru still doesn’t understand why read the following article

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

This article introduces three functions for url encoding escape(), encodeURI( ), encodeURIComponent()

escape() method:
Encode the specified string using the ISO Latin character set. All spaces, punctuation marks, special characters and other non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character's encoding in the character set table). For example, the encoding corresponding to the space character is %20. The unescape method is the opposite. Characters that will not be encoded by this method: @ * / +

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

encodeURIComponent() method:
Convert the URI string into UTF-8 encoding format A string in escape format. Compared with encodeURI(), this method will encode more characters, such as / and other characters. So if the string contains several parts of the URI, you cannot use this method to encode, 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, if you do not want to convert the string encoding format into UTF-8 format (such as the original page When the charset is consistent with the target page), you only need to use escape. If your page is GB2312 or other encoding, and the page that accepts parameters is UTF-8 encoded, you must use encodeURI or encodeURIComponent.

Note: The escape method cannot be used to encode Uniform Resource Identifiers (uri). To encode it, use the encodeuri and encodeuricomponent methods. In addition, encodeURI/encodeURIComponent was introduced after javascript1.5, and escape was available in javascript1.0.

To summarize 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. When the content encoded in this way reaches the background, the characters are all in UTF-8 encoding format;
3. If your Servlet/ The Content-Type specified by JSP for output is UTF-8. Congratulations, you do not need to perform special encoding or decoding operations on these Parameters, it must be normal;
4. Otherwise, you need to decode these parameters. Encoding operation, for example, if your page is GBK encoded, then you need to write like this:
String sPara = new String(request.getParamter("test").getBytes("iso-8859-1"),"GBK") ;
GBK encoding can also be used if gb2312 encoding is used;
5. If you are working in eclipse, please pay attention to the properties of your Servlet and JSP files. The encoding must be the same as that set in content-type Same, otherwise javac will encode your file incorrectly. At this time, the string of the bytecode file itself will be wrong, and the output will not be useful;
6. Finally, make a summary:
6.1. The encoding of the servlet/JSP file itself must be consistent with the content-type output, otherwise additional encoding and decoding steps need to be added;
6.2. The encoding passed in the HTTP/POST method and encodeURI method are all UTF-8;
6.3. Ordinary window.open(...), that is, HTTP/GET method, the incoming encoding is consistent with the encoding of the page running the script;
6.4. Background decoding must pass iso-8859- 1 to decode, and then use your target encoding to encode;
6.5, response.setContentType("text/xml;charset=utf-8");
If there are Chinese characters echoed to the page, charset= must be added utf-8 characters
request.setCharacterEncoding("UTF-8");//If the page comes through POST and contains Chinese characters, you must add this sentence

This statement is placed at the front of the program;

6.6 Please ensure that the project attribute encoding is "UTF-8", and the request page and response page encoding are consistent (can be background) UTF-8. You must ensure that the file

attribute and ContentType settings are consistent

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325087.htmlTechArticleAfter the encodeURIComponent function in JS encodes Chinese, how to decode it with php? ? Premise: The Chinese before encoding may be gbk, gb2312, utf-8, etc. Copy the code The code is as follows: urldecode() iconv() in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn