PHP Chinese URL codec (urlencode () Rawurlencode () _php Tips

Source: Internet
Author: User
Tags form post urlencode alphanumeric characters
Here is a detailed explanation:///\\\
String UrlEncode (String str)
Returns a string, in addition to the-_ in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-bit hexadecimal number, and the space is encoded as a plus (+). This encoding is the same encoding 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 ()) for encoding spaces as plus signs (+). This function makes it easy to encode the string and use it for the request part of the URL, and it also makes it easy to pass the variable to the next page: Example 1. UrlEncode () example
Copy Code code as follows:

<?php
Echo ' <a href= ' mycgi?foo= ', UrlEncode ($userinput), ' > ';
?>

Note: Be careful with the variables that match the HTML entity. such as &, ©, and £ will be parsed by the browser, using the actual entity instead of the desired variable name. This is a clear mess, and the consortium has been telling people for years. Reference address: http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2 PHP through the arg_separator. INI directive, support to change the parameter delimiter to the recommended semicolon for the consortium. Unfortunately, most user agents do not send form data in semicolon-delimited format. A simpler solution is to use & instead of & As a separator. You don't need to modify PHP's arg_separator for this. Keep it still, and use Htmlentities (UrlEncode ($data) to encode your URL.
Example 2. UrlEncode () and htmlentities () examples
Copy Code code as follows:

<?php
Echo ' <a href= ' mycgi?foo= ', Htmlentities (UrlEncode ($userinput)), ' > ';
?>


String UrlEncode (String str)
Returns a string, in addition to the-_ in this string. All non-alphanumeric characters are replaced with a percent semicolon (%) followed by a two-bit hexadecimal number. This is the encoding described in RFC 1738 to protect the literal character from being interpreted as a special URL delimiter, while protecting the URL format from being confused by the transfer media (like some messaging systems) using character conversions. For example, if you want to include a password in the FTP URL:
Example 1. Rawurlencode () Example 1
Copy Code code as follows:

<?php
Echo ' <a href= ' ftp://user: ', Rawurlencode (' foo @+%/'),
' @ftp. My.com/x.txt ' > ';
?>


Or, if you want to pass the message through the Path_info component of the URL:
Example 2. Rawurlencode () Example 2
Copy Code code as follows:

<?php
Echo ' <a href= ' http://x.com/department_list_script/',
Rawurlencode (' Sales and Marketing/miami '), ' > ';
?>


When decoding, you can use the corresponding UrlDecode () and Rawurldecode (), and accordingly, Rawurldecode () will not decode the plus sign (' + ') as a space, and UrlDecode () can. The following is a detailed example:
String UrlDecode (String str)
Decodes any%## in the encoded string given. Returns the decoded string. Example 1. UrlDecode () example
Copy Code code as follows:

<?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 sequence of a percent semicolon (%) followed by a two-digit hexadecimal number is replaced with a literal character.
Example 1. Rawurldecode () example
Copy Code code as follows:

<?php
echo rawurldecode (' Foo%20bar%40baz '); Foo Bar@baz
?>

However, one thing to note is that UrlDecode () and Rawurldecode () decoded strings are encoded in the UTF-8 format, if the URL contains Chinese words, and the page settings are not UTF-8 words, the decoded string to be converted to normal display!
Another problem is that the URL that is obtained is not the format of the%%nn N={0..F}, but the format of the%unnnn N={0..F}, at which point the UrlDecode () and Rawurldecode () are not decoded correctly, and the following function is used to correctly decode :
Copy Code code 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;
}

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.