PHP UrlEncode () and UrlDecode () Letter coding principle of detailed _php skills

Source: Internet
Author: User
Tags chr form post ord urlencode alphanumeric characters
The principle is to convert the Chinese characters to hexadecimal and according to some rules of string combination, to achieve the encoding and decoding of characters, to ensure the integrity and compatibility of the URL data transmission process, mainly discusses the encoding of Chinese characters.

one, Firefox browser encoding Chinese characters

If you enter a Chinese character in the Firefox browser, URL encoding will be implemented automatically, as follows

Before pressing the ENTER key

When you press the ENTER key


two, UrlEncode () function principle

The UrlEncode () function is used to encode the URL string, which mainly discusses the encoding of Chinese characters.

Examples are as follows
Copy Code code as follows:

Echo UrlEncode (' Don't crush on elder brother ');/output:%b2%bb%d2%aa%c3%d4%c1%b5%b8%e7

The principle of UrlEncode () is to first convert the Chinese character to 16, and then add an identifier% before each character to understand this principle, you can implement a custom URL encoding function, the following code
Copy Code code as follows:

$string = "Don't be infatuated with brother";
$length = strlen ($string);
Echo $string;
$result = Array ();
Decimal
for ($i =0; $i < $length; $i + +) {
if (Ord ($string [$i]) >127) {
$result [] = Ord ($string [$i]). ' '. Ord ($string [+ + $i]);
}
}
Var_dump ($result);
Hexadecimal
$strings = Array ();
foreach ($result as $v) {
$dec = Explode ("", $v);
$strings [] = "%". Dechex ($dec [0]). " "." % ". Dechex ($dec [1]);
}
Var_dump ($strings);

The above code in the [PHP implementation of character into the conversion principle analysis] in the article in the text of the hexadecimal principle analysis section has been discussed in detail, the function of the UrlEncode () function is realized by acquiring each character of the Chinese character and then converting it to 16, and adding a special identifier% before each character. The output is as follows
The results of the output are then compared to the characters that are encoded directly using UrlEncode (), as above:%b2%bb%d2%aa%c3%d4%c1%b5%b8%e7

The above example shows that using the UrlEncode () function to encode Chinese characters is essentially to convert the character to hexadecimal and then add a special identifier% to the left of the first character.

three, UrlDecode () function principle

The encoded URL string is decoded using the UrlDecode () function, as follows

echo urldecode ('%b2%bb%d2%aa%c3%d4%c1%b5%b8%e7 ');/output: Don't be obsessed with brother.
The UrlDecode () function, in contrast to the UrlEncode () function, decodes an encoded URL string by converting a hexadecimal string into a Chinese character, combining the above example to implement a custom function decoding string
Copy Code code as follows:

$string = '%b2%bb%d2%aa%c3%d4%c1%b5%b8%e7 ';
$length = strlen ($string);
$hexs = Array ();
for ($i =0; $i < $length; $i + +) {
if ($string [$i] = = '% ') {
$hexs [] = $string [+ + $i]. $string [+ + $i];
}
}
$num = count ($hexs);
for ($i =0; $i < $num; $i + +) {
Echo Chr (Hexdec ($hexs [$i])). Chr (Hexdec ($hexs [+ + $i]));
}

The example code above first extracts the hexadecimal characters of each character by the rules of the string, and then uses the Hexdec () function to convert the 16 to decimal, and then use the Chr () function to convert the decimal to a character and 16 to a character. The output is as follows

four, UrlDecode () and UrlEncode () function Description

UrlEncode
(PHP 3, PHP 4, PHP 5)
UrlEncode--coded URL string
Description
String UrlEncode (String str)
Returns a string, in addition to the-_ in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-bit hexadecimal number, and the space is encoded as a plus (+). This encoding is the same encoding as the WWW form POST data and is encoded in the same way as the application/x-www-form-urlencoded media type. For historical reasons, this encoding differs from RFC1738 encoding (see Rawurlencode ()) for encoding spaces as plus signs (+). This function makes it easy to encode the string and use it for the request part of the URL, and it also makes it easy to pass the variable to the next page

UrlDecode
(PHP 3, PHP 4, PHP 5)
UrlDecode--Decoding the encoded URL string
Description
String UrlDecode (String str)
Decodes any%## in the encoded string given. Returns the decoded string.

Five, reference resources
UrlEncode () description
UrlDecode () description

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.