URL of urlencode of rawurlencode of

  • 2020-03-31 20:59:17
  • OfStack

The following is the detailed explanation: ///\\\
String urlencode (string STR)
Returns a string in which all non-alphanumeric characters except -_. Are replaced with percent (%) followed by two hexadecimal digits, and Spaces are encoded as plus (+). This code is the same as the WWW form POST data and the same as the application/x-www-form-urlencoded media type. For historical reasons, this code differs from the RFC1738 code in encoding a space as a plus (+) (see rawurlencode()). This function makes it easy to encode the string and use it for the requested part of the URL, and it also makes it easy to pass the variable to the next page: example 1. Urlencode () example
 
<?php 
echo '<a href="mycgi?foo=', urlencode($userinput), '">'; 
?> 

Note: beware of variables that match HTML entities. Things like &, ©, and £ are parsed by the browser and the expected variable names are replaced with actual entities. This is an obvious mess, and the W3C has been warning about it for years. Reference address: http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2 PHP by arg_separator ini directives, support the parameter separator into a W3C recommended a semicolon. Unfortunately, most user agents do not send form data in semicolon delimiter format. The easier solution is to use & Instead of & as the separator. You do not need to change PHP's arg_separator for this purpose. Leave it ampersand, but just use htmlentities(urlencode($data)) to encode your URL.
Example 2. Urlencode () and htmlentities() examples
 
<?php 
echo '<a href="mycgi?foo=', htmlentities(urlencode($userinput)), '">'; 
?> 


String urlencode (string STR)
Returns a string in which all non-alphanumeric characters except -_. Are replaced with percent (%) followed by two hexadecimal digits. This is the encoding described in RFC 1738 to protect the literal character from being interpreted as a special URL delimiter, and to protect the URL format from being confused by the character conversion used by transport media (like some mail systems). For example, if you want to include a password in an FTP URL:
Example 1. Example 1 of rawurlencode()
 
<?php 
echo '<a href="ftp://user:', rawurlencode('foo @+%/'), 
'@ftp.my.com/x.txt">'; 
?> 


Or, if you want to pass information through the PATH_INFO component of the URL:
Example 2. Rawurlencode () example 2
 
<?php 
echo '<a href="http://x.com/department_list_script/', 
rawurlencode('sales and marketing/Miami'), '">'; 
?> 


When decoding, the corresponding urldecode() and rawurldecode() can be used. Correspondingly, rawurldecode() does not decode the plus sign ('+') as a space, whereas urldecode() can. Here is a detailed example:
String urldecode (string STR)
Any %## in the encoded string given by the decode. Returns the decoded string. Urldecode () example
 
<?php 
$a = explode('&', $QUERY_STRING); 
$i = 0; 
while ($i < count($a)) { 
$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 in which the percent sign (%) followed by a sequence of two hexadecimal Numbers are replaced with literal characters.
Example 1. Example of rawurldecode()
 
<?php 
echo rawurldecode('foo%20bar%40baz'); // foo bar@baz 
?> 

However, it is important to note that urldecode() and rawurldecode() decode strings encoded in utf-8 format, if the URL contains Chinese and the page Settings are not utf-8, then the decoded string must be converted to display properly!
Another problem is that the URL obtained is not %%nn n={0.. F} format, but %unnnn n={0.. F} format, then using urldecode() and rawurldecode() will not decode correctly, but need to use the following function to decode correctly:
 
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; 
} 

Related articles: