Home > Backend Development > PHP Tutorial > Detailed explanation of the character encoding principle of php urlencode() and urldecode() functions_PHP tutorial

Detailed explanation of the character encoding principle of php urlencode() and urldecode() functions_PHP tutorial

WBOY
Release: 2016-07-21 15:22:30
Original
1067 people have browsed it

The principle is to convert Chinese characters into hexadecimal and combine strings according to certain rules to achieve encoding and decoding of characters to ensure the integrity and compatibility of characters during URL data transmission. It mainly discusses the encoding of Chinese characters. Condition.

1. FireFox browser encodes Chinese characters

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

Before pressing the Enter key
php-internet-url-encode-before
After pressing the Enter key
php-internet-url-encode

2. Principle of urlencode() function

The urlencode() function is used to encode URL strings. Here we mainly discuss the encoding of Chinese characters.

The example is as follows

Copy codeThe code is as follows :

echo urlencode('Don't be obsessed with me');//Output: %B2%BB%D2%AA%C3%D4%C1%B5%B8%E7

The principle of the urlencode() function is to first convert Chinese characters into hexadecimal, and then add an identifier % in front of each character. Understand this principle, you can implement a custom URL encoding function, the code is as follows
Copy code The code is as follows:

$string = "Don't be obsessed with me";
$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);
//Hex
$strings = array();
foreach($result as $v){
$dec = explode(" ",$v);
$strings[] = "%".dechex($dec[0])." "."%".dechex($dec[1]);
}
var_dump($strings);

php-self-urlencode

The above code is discussed in detail in the analysis of the principle of converting Chinese characters to hexadecimal in the article [PHP Implementation of Chinese Character Base Conversion Principle]. By obtaining each character of the Chinese character and then converting it to hexadecimal base, and add a special identifier % in front of each character to realize the function of urlencode() function. The output result is as follows
Then compare the output result with the characters encoded directly using urlencode() , as above: %B2%BB%D2%AA%C3%D4%C1%B5%B8%E7

As can be seen from the above example, using the urlencode() function to encode Chinese characters is essentially converting the characters into ten Hexadecimal adds a special identifier % to the left of the first character

3. Principle of the urldecode() function

Use the urldecode() function to decode the encoded URL string, the example is as follows

echo urldecode('%B2%BB%D2%AA%C3%D4%C1%B5%B8%E7');//Output: Don’t be obsessed with me
The urldecode() function has the opposite principle to the urlencode() function. It is used to decode encoded URL strings. Its principle is to convert hexadecimal strings into Chinese characters. Combined with the above example, a custom function can also be used to decode strings.
Copy code The 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 example code first extracts the hexadecimal of each character according to the rules of the string, then uses the hexdec() function to convert the hexadecimal to decimal, and then uses the chr() function to convert the decimal to characters to achieve Convert hexadecimal to characters. The output is as follows

php-self-urldecode

4. Description of urldecode() and urlencode() functions

urlencode
(PHP 3, PHP 4, PHP 5)
urlencode -- Encode URL string
Description
string urlencode (string str)
Returns a string, all characters in this string except -_. Non-alphanumeric characters are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as a plus sign (+). This encoding is the same as the encoding of WWW form POST data, and the same encoding as the application/x-www-form-urlencoded media type. For historical reasons, this encoding differs from the RFC1738 encoding (see rawurlencode()) in encoding spaces as plus signs (+). This function facilitates encoding a string and using it in the request part of the URL, and it also facilitates passing variables to the next page

urldecode
(PHP 3, PHP 4 , PHP 5)
urldecode -- Decode an encoded URL string
Description
string urldecode ( string str )
Decode any %## in the given encoded string. Returns the decoded string.

5. Reference resources
urlencode() description
urldecode() description

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324688.htmlTechArticleThe principle is to convert Chinese characters into hexadecimal and combine strings according to certain rules to realize characters Encoding and decoding to ensure the integrity and compatibility of characters during URL data transmission...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template