Home  >  Article  >  Backend Development  >  Introduction to the difference between php urlencode and rawurlencode_PHP tutorial

Introduction to the difference between php urlencode and rawurlencode_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:05966browse

In PHP, both rawurlencode and rawurlencode encode characters. Let me introduce to you the difference between urlencode and rawurlencode. Friends who need to know can refer to it.

The purpose of urlencode is to encode a string and replace all non-alphanumeric characters except "-_" in the original string with a percent sign (%) followed by two hexadecimal digits. However, please note: For historical reasons, spaces will be replaced with + signs. Rawurlencode is actually the same as urlencode, and is also used to encode strings. The only difference is that it uses RFC1738 encoding, which means that spaces are replaced with %20.

Their corresponding decoding functions are urldecode and rawurldecode. Referring to the instructions on the official website, any %## and plus sign ('+') in the encoded string given by urldecode decoding are decoded into a space character; rawurldecode decodes the percent sign (%) in the character string followed by two Bit hexadecimal. There are two differences. First, urldecode will decode any two characters after the percent sign (%). For example, %MN will also be decoded, but it will fail; rawurldecode will only decode the percent sign (%). Only the last two characters in hexadecimal (0-9A-F) will be decoded. Second, urldecode will decode the + sign into a space.

Let’s take a look at the official introduction of the two functions in PHP.

urlencode — encode a URL string

Report a bug Description
string urlencode ( string $str )
This function makes it easy to encode a string and use it in the request part of the URL, and it also makes it easy to pass variables to the next page.

Report a bug parameter

str
The string to encode.

Report a bug return value
Returns a string in which all non-alphanumeric characters except -_. are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs (+). 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 (+).

string rawurlencode ( string $str ) Characters specified according to » RFC 3986 encoding.

The code is as follows
 代码如下 复制代码

Example #1 urlencode() 例子

echo '';
?>


Example #2 urlencode() 与 htmlentities() 例子

$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '
';
?>

Copy code

Example #1 urlencode() Example

echo '';
?>

Example #2 urlencode() and htmlentities() example


$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);

echo '';
?>

rawurlencode — Encode URLs according to RFC 1738
 代码如下 复制代码

Example #1 在 FTP URL 里包含一个密码

echo '';
?>
以上例程会输出:

Report a bug Description
Report a bug parameter str The URL to encode. Report a bug return value Returns a string in which all non-alphanumeric characters except -_. are replaced with a percent sign (%) followed by two hexadecimal digits. This is an encoding described in » RFC 3986, which is intended to protect literal characters from being interpreted as special URL delimiters, and to protect the URL format from being garbled by character conversions used by the transport medium (like some mail systems) . Example
The code is as follows Copy code
Example #1 Include a password in the FTP URL echo ''; ?> The above routine will output: From the above description of the decoding function, it can be inferred that anything encoded using urlencode or rawurlencode can be decoded using urldecode. However, if the original string contains spaces, the string encoded with urlencode will be obtained after decoding with rawurlencode. The string will be different from the original string.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628888.htmlTechArticleIn php, rawurlencode and rawurlencode both encode characters. Let me introduce urlencode and rawurlencode to you. Friends who need to know the difference can refer to it. The use of urlencode...
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