PHP UrlEncode () function implements URL encoding parsing _php tutorial

Source: Internet
Author: User
Tags form post rar urlencode alphanumeric characters
in PHP UrlEncode () function is to convert the Chinese into a string encoding, so that does not cause the Chinese URL in the browser open garbled or error problems, let me introduce you in the use of UrlEncode () coding is some specific details.

UrlEncode
(PHP 4, PHP 5)

urlencode-encoded URL string

Report a bug description
String UrlEncode (String $str)
This function makes it easy to encode a string and use it for the request part of the URL, and it also facilitates the passing of a variable to the next page.

Report a bug parameter

Str
The string to encode.

Report a bug return value

Returns a string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+). This encoding is the same 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 ()) in terms of encoding spaces as plus signs (+).

is UrlEncode:

This tool realizes the encode and decode of two ways respectively.

Encode,%D6%D0%CE%C4, GB2312, Chinese
Encode,%e4%b8%ad%e6%96%87, UTF-8, Chinese

UrlEncode in HTML:
In an HTML file encoded as GB2312,
/Chinese. RAR---browser automatically converted to/%d6%d0%ce%c4.rar
Note: Firefox does not support the Chinese URL of GB2312 encode because it defaults to UTF-8 encoding to send the URL, but the ftp://protocol is OK, I tried it. I think it's a bug in Firefox.

In an HTML file encoded as UTF-8,
/Chinese. RAR---browser automatically converted to/%e4%b8%ad%e6%96%87.rar

UrlEncode in PHP:

The code is as follows Copy Code

GB2312 's Encode
Echo UrlEncode ("Chinese-_."). " n "; %d6%d0%ce%c4-_.+
Echo UrlDecode ("%d6%d0%ce%c4-_."). " n "; Chinese-_.
Echo Rawurlencode ("Chinese-_."). " n "; %d6%d0%ce%c4-_.%20
Echo Rawurldecode ("%d6%d0%ce%c4-_."). " n "; Chinese-_.
?>

Except-_. All non-alphanumeric characters are replaced with a percent (%) followed by a two-digit hexadecimal number.
The difference between UrlEncode and Rawurlencode:
UrlEncode the space is encoded as a plus sign (+)
Rawurlencode the space is encoded as a plus sign (%20)

If you want to use UTF-8 's encode, there are two ways:


One, save the file as UTF-8 file, directly use UrlEncode, Rawurlencode can.
Second, use the Mb_convert_encoding function.

The code is as follows Copy Code

$url = '/Chinese. rar ';
Echo UrlEncode (mb_convert_encoding ($url, ' utf-8′, ' gb2312′). " n ";
Echo Rawurlencode (mb_convert_encoding ($url, ' utf-8′, ' gb2312′). " n ";
Http%3a%2f%2fud03.kinoko.name%2f%e4%b8%ad%e6%96%87.rar
?>

Instance:

The code is as follows Copy Code

function parseURL ($url = "")
{
$url = Rawurlencode (mb_convert_encoding ($url, ' gb2312′, ' utf-8′));
$a = Array ("%3a", "%2f", "%40″");
$b = Array (":", "/", "@");
$url = Str_replace ($a, $b, $url);
return $url;
}
$url = "ftp://ud03:password@ud03.kinoko.name/Chinese/English. rar";
echo parseURL ($url);
Ftp://ud03:password@ud03.kinoko.name/%d6%d0%ce%c4/%d6%d0%ce%c4.rar
?>

UrlEncode in javascript:

The code is as follows Copy Code

%e4%b8%ad%e6%96%87-_.%20%e4%b8%ad%e6%96%87-_.%20

encodeURI does not encode the following characters: ":", "/", ";", "?", "@" and other special characters.

The code is as follows Copy Code

/%e4%b8%ad%e6%96%87.rarhttp%3a%2f%2fud03.kinoko.name%2f%e4%b8%ad%e6%96%87.rar


Note:

Note: Be careful with variables that match the HTML entity. such as &, ©, and £ will be parsed by the browser and replaced with the actual entity with the expected variable name. This is a clear confusion, and the crowd has been telling people for years. Reference address:»http://www.w3.org/tr/html4/appendix/notes.html#h-b.2.2.

PHP supports the use of the Arg_separator.ini directive to turn the parameter delimiter into a semicolon suggested by the consortium. Unfortunately, most user agents do not send form data in a semicolon-delimited format. A simpler solution is to use & instead of & As a delimiter. You don't need to modify PHP's arg_separator for this. Make it still &, and use only htmlentities () or htmlspecialchars () to encode your URL.

http://www.bkjia.com/PHPjc/632093.html www.bkjia.com true http://www.bkjia.com/PHPjc/632093.html techarticle in PHP urlencode () function is to convert the Chinese into a string encoding, so that does not cause the Chinese URL in the browser open garbled or error problems, I would like to introduce you in the ...

  • 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.