PHP UrlEncode () and UrlDecode () function digital character encoding principle _php Tutorial

Source: Internet
Author: User
Tags binary to decimal form post alphanumeric characters
The principle is to convert Chinese characters into hexadecimal and string combinations according to some rules, to realize character encoding and encoding, to ensure the integrity and compatibility of characters in the process of URL data transmission, and to discuss the encoding of Chinese character.

one, the Firefox browser encoding Chinese characters

In the Firefox browser, if you enter Chinese characters, URL encoding will be automatically implemented as follows

Before pressing the ENTER key

After pressing 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 CodeThe code is as follows:
Echo UrlEncode (' Don't crush brother ');//output:%b2%bb%d2%aa%c3%d4%c1%b5%b8%e7

The principle of the UrlEncode () function is to first convert the Chinese characters to 16, and then precede each character with an identifier%, understand this principle, you can implement a custom URL encoding function, the code is as follows
Copy CodeThe code is as follows:
$string = "Don't be infatuated with elder 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 the text character conversion principle analysis] in the text character to the hex principle analysis part of the article is discussed in detail, by acquiring the characters of the character is converted to 16, and in front of each character with a special identifier%, the realization of the function of the UrlEncode () function, The output is as follows
The results of the output are then compared with characters directly encoded with 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 converting the character to hexadecimal and adding a special identifier to the left of the first character.

three, UrlDecode () function principle

Use the UrlDecode () function to decode the encoded URL string, as in the following example

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, is used to decode the encoded URL string by converting the hexadecimal string to Chinese characters, which, in conjunction with the above example, can also implement a custom function decoding string
Copy CodeThe code is 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 above instance code first extracts the hexadecimal of each character by the rules of the string, and then uses the Hexdec () function to convert the 16 binary to decimal, and then uses the Chr () function to convert the decimal to a character and to convert the 16 binary to a character. The output is as follows

four, UrlDecode () and UrlEncode () function Description

UrlEncode
(PHP 3, PHP 4, PHP 5)
UrlEncode--encoded URL string
Description
String UrlEncode (String str)
Returns a string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+). This encoding is the same 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 ()) in terms of encoding spaces as plus signs (+). This function makes it easy to encode a string and use it for the request part of a URL, and it also facilitates passing a variable to the next page

UrlDecode
(PHP 3, PHP 4, PHP 5)
UrlDecode-decode 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

http://www.bkjia.com/PHPjc/324688.html www.bkjia.com true http://www.bkjia.com/PHPjc/324688.html techarticle the principle is to convert Chinese characters into hexadecimal and string combinations according to some rules, to realize character encoding and encoding, and to ensure the integrity of characters in the process of URL data transmission .

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