The following is a detailed explanation :///\\\
String urlencode (string Str)
Returns a string -_. all other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces will be encoded as the plus sign (+ ). This encoding method is the same as that for WWW form post data and the same as that for application/X-WWW-form-urlencoded. For historical reasons, this encoding is different from rfc1738 encoding (see rawurlencode () in Space Encoding As the plus sign (+. This function allows you to encode a string and use it in the URL request section. It also allows you to pass variables to the next page: Example 1. urlencode () Example
CopyCode The Code is as follows: <? PHP
Echo '<a href = "mycgi? Foo = ', urlencode ($ userinput),' "> ';
?>
Note: Be careful with the variables that match the HTML object. The image & amp, & copy, and & pound will be parsed by the browser, and the expected variable name will be replaced by the actual object. This is obviously confusing. W3C has warned people for years. Reference address: http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2 PHP uses the arg_separator. ini command to change the parameter delimiter to the semicolon recommended by W3C. Unfortunately, most user proxies do not send form data in the semicolon separator format. A simple solution is to use & replace & as the separator. You do not need to modify the PHP arg_separator. Make it still &, and use only htmlentities (urlencode ($ data) to encode your URL.
Example 2. example of urlencode () and htmlentities () copy Code the code is as follows: echo ' ';
?>
String urlencode (string Str)
Returns a string. All non-alphanumeric characters except-_. In this string will be replaced with a semicolon (%) followed by two hexadecimal numbers. This encoding is described in RFC 1738 to protect the original characters from being interpreted as special URL delimiters and protect the URL format to prevent them from being transmitted to media (like some email systems) use character conversion. For example, if you want to include a password in an ftp url:
Example 1. rawurlencode () Example 1Copy codeThe Code is as follows: <? PHP
Echo '<a href = "ftp: // User:', rawurlencode ('foo @ + % /'),
'@ Ftp.my.com/x.txt "> ';
?>
Or, if you want to pass the information through the path_info component of the URL:
Example 2. rawurlencode () Example 2Copy codeThe Code is as follows: <? PHP
Echo '<a href = "http://x.com/department_list_script /',
Rawurlencode ('sales and marketing/Miami '),' "> ';
?>
During decoding, you can use the corresponding urldecode () and rawurldecode (). Correspondingly, rawurldecode () does not decode the plus sign ('+') as a space, while urldecode () can. The following is a detailed example:
String urldecode (string Str)
Decodes any % # In the encoded string ##. Returns the decoded string. Example 1. urldecode () ExampleCopy codeThe Code is as follows: <? PHP
$ A = explode ('&', $ QUERY_STRING );
$ I = 0;
While ($ I <count ($ )){
$ B = Split ('=', $ A [$ I]);
Echo 'value for parameter ', htmlspecialchars (urldecode ($ B [0]),
'Is, htmlspecialchars (urldecode ($ B [1]), "<br/> N ";
$ I ++;
}
?>
String rawurldecode (string Str)
Returns a string of hundreds of semicolons (%) followed by two hexadecimal numbers. All strings are replaced with original characters.
Example 1. rawurldecode () ExampleCopy codeThe Code is as follows: <? PHP
Echo rawurldecode ('foo % 20bar % 40baz'); // Foo bar @ Baz
?>
However, one thing to note is that the strings decoded by urldecode () and rawurldecode () are encoded in the UTF-8 format, if the URL contains Chinese characters, and the page settings is not the UTF-8, the decoded string to convert, In order to display normally!
Another problem is that the obtained URL is not % NN n = {0 .. f} format, but % unnnn n = {0 .. f} format. At this time, using urldecode () and rawurldecode () cannot be decoded correctly, but the following function can be used to decode it correctly: Copy code The Code is as follows: function utf8rawurldecode ($ source)
{
$ Decodedstr = "";
$ Pos = 0;
$ Len = strlen ($ source );
While ($ POS <$ Len ){
$ Charat = substr ($ source, $ POs, 1 );
If ($ charat = '% '){
$ POS ++;
$ Charat = substr ($ source, $ POs, 1 );
If ($ charat = 'U '){
// We got a Unicode Character
$ POS ++;
$ Unicodehexval = substr ($ source, $ POs, 4 );
$ Unicode = hexdec ($ unicodehexval );
$ Entity = "". $ Unicode .';';
$ Decodedstr. = utf8_encode ($ entity );
$ POS + = 4;
}
Else {
// We have an escaped ASCII character
$ Hexval = substr ($ source, $ POs, 2 );
$ Decodedstr. = CHR (hexdec ($ hexval ));
$ POS + = 2;
}
} Else {
$ Decodedstr. = $ charat;
$ POS ++;
}
}
Return $ decodedstr;
}