Encodeuri and encodeuricomponent Functions

Source: Internet
Author: User

When data is transmitted using strings on the Network, some strings must be transcoded because they have special meanings. This mechanism is inevitable. We need any character as the data, and no character is only used as the control character, not as the data to be transmitted. The most basic transcoding method is to use quotation marks to indicate the backslash escape when a string is used. Without this escape rule, strings cannot be distinguished from the code. No matter what character you choose as the string identifier, to indicate this identifier, a string must be escaped. At least two escape characters must be required, one is the string's expression and the other is the escape character.


The network protocol requires more controllers. encodeuri is used to transcode the strings to prevent transmission errors, all transcoding functions use ASCII code less than 128 characters to encode a large number of character sets. After all, such "primitive" characters are handy in various scenarios.

In fact, the encodeuri function does not encode only 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z

Later, because someone uses & = to connect the URL string for data transmission, including post and get, this method is used, so "?", "&" And "=" are also controllers and need to be transcoded. for other reasons, two levels of transcoding are required. This function is encodeuricomponent, encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, a-Z.


In this way, encodeuricomponent is used to transcode the data to be transmitted, And then ?, &, = Is connected. Generally, the string may be automatically encoded once before transmission. Therefore, encodeuri must not transcode these three characters, and the encodeuricomponent function must transcode these three characters.


PHP has a similar urlencode (inverse urldecode) function, but this function is more thorough, it will remove ". _-"transcode all characters other than the three characters (of course, there are uppercase and lowercase letters and 10 numbers, and all encoding functions do not transcode them ).


In fact, the encoding rules used by these functions are the same, that is, the encoding is indicated by "%" and decoded by the hex (hexadecimal) number. Therefore, theoretically, this rule can be used to encode and decode any character. The difference between the rules is the encoding and non-encoding. Therefore, in theory, decoding is the same function. However, this is not the case. encodeuricomponent and PHP urlencode can be decoded to each other (this is incorrect), while encodeuri is different, the dencodeuricomponent function decodes non-encoded characters starting with "%.


Supplement: encodeuricomponent and PHP's urlencode cannot be decoded to each other. In fact, there is no need to encode and decode each other between the client and the server, and they can be run independently.As mentioned above,
Encodeuri and encodeuricomponent are mainly used to connect post and get data. of course, we know that general webpage data requests are automatically transcoded internally, but this is generally not required, including Chinese. transcoding is not required when post or get data is connected, the purpose of transcoding is to convert & = to avoid unexpected data when the server parses $ _ post and $ _ get.

Therefore:

1. Although the data we send is encoded, the $ _ post and $ _ get data on the server does not need to be decoded because it has been automatically parsed.

2. when the server outputs characters, urlencode transcoding is not required, because the client has automatically completed the conversion when receiving the characters. if urlencode is used on the server to encode the data, there is no ready-made function to decode the data using JavaScript, and decodeuricomponent must be used to decode the data, although most of the time it is correct, however, some characters may encounter errors,
For example, a space is converted into a + number.


The form has an encripy option. Its default value is application/X-WWW-form-urlencoded (Ajax also has this option), that is, urlencode encoding of the content, javaScript does not have this encoding function. It is actually the urlencode function of PHP. when the data is uploaded to the server, _ Get or _ post has been decoded by itself.

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.