Phpurlencode () function implements URL encoding parsing _ PHP Tutorial

Source: Internet
Author: User
Tags form post alphanumeric characters
The phpurlencode () function implements URL encoding parsing. In php, the urlencode () function converts Chinese characters to string encoding, so that Chinese URLs are not prone to garbled characters or errors in the browser, the following describes how to convert the urlencode () function in php into string encoding, this will not cause the Chinese url to be opened in the browser or errors. I will introduce the specific details of using urlencode () encoding.

Urlencode
(PHP 4, PHP 5)

Urlencode-encode a URL string

Report a bug description
String urlencode (string $ str)
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.

Report a bug parameters

Str
The string to be encoded.

Report a bug return value

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 (+.

What is URLEncode:

This tool implements Encode and Decode in two ways respectively.

Chinese-> GB2312 Encode-> % D6 % D0 % CE % C4
English-> UTF-8 Encode-> % E4 % B8 % AD % E6 % 96% 87

URLEncode in Html:
In an html file encoded as GB2312,
/.Rar-> automatically convert the browser to->/%D6%D0%CE%C4.rar
Note: Firefox does not support the Chinese URL of GB2312 Encode, because it is the UTF-8 code by default to send the URL, but ftp: // protocol can, I tried. I think this is a Firefox bug.

In an html file encoded as a UTF-8,
/.Rar-> automatically convert the browser to->/%E4%B8%AD%E6%96%87.rar

URLEncode in PHP:

The code is as follows:

// GB2312 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 -_.
?>

All non-alphanumeric characters except-_. will be replaced with a semicolon (%) followed by two hexadecimal numbers.
The difference between urlencode and rawurlencode:
Urlencode encodes a space into a plus sign (+)
Rawurlencode encodes a space into a plus sign (% 20)

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


1. Save the file as a UTF-8 file, directly use urlencode, rawurlencode.
2. use the mb_convert_encoding function.

The code is as follows:

$ Url = '/ .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:

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/ .rar ";
Echo parseurl ($ url );
// Ftp: // ud03: password@ud03.kinoko.name/% D6 % D0 % CE % C4/127d6%d0%ce%c4.rar
?>

URLEncode in JavaScript:

The code is as follows:

% E4 % B8 % AD % E6 % 96% 87-_. % 20% E4 % B8 % AD % E6 % 96% 87-_. % 20

EncodeURI does not encode the following characters: ":", "/", ";", "?" And.

The code is as follows:

/Configure


Note:

Note: Be careful with the variables that match the HTML object. Image &,©And £4 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 convert 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 () or htmlspecialchars () to encode your URL.

The transform () function converts Chinese characters to string encoding, so that Chinese URLs do not cause garbled characters or errors in the browser, next I will introduce you to the tutorial...

Related Article

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.