PHP urlencode ()

Source: Internet
Author: User
Tags alphanumeric characters

In PHP, functions such as urlencode (), urldecode (), rawurlencode (), and rawurldecode () are provided to solve the web page.
URL encoding and decoding.

In ASP, URL encoding and decoding are very annoying, and server. urlencode is not very easy to use. In case of UTF-8 encoding
Annoying. You need to get keywords from the URL links of Baidu and Google websites, and write a bunch of user-defined functions to get the urldecode effect.

Extract an article about
The article on PHP urlencode () functions provides a comprehensive understanding of php URL Processing. The article comes from the top 13 in the 373ren ranking. Thank you.

Understanding
Urlencode

:

Urlencode: a pointer to the Chinese characters in the URL of a webpage. The most common method is Baidu,
When Google and other search engines enter Chinese characters for query
The URL of the webpage that you have accessed. Urlencode can be either of the traditional gb2312-based encode (Baidu, yisou, etc.) or
UTF-8-based encode (Google, Yahoo, etc ). This tool implements encode and decode in two ways respectively.

Chinese
-> Encode-> % D6 % D0 % Ce % c4 of gb2312
Chinese> UTF-8 encode>
% E4 % B8 % ad % E6 % 96% 87

Urlencode in HTML
:

In the HTML file encoded as gb2312:

Http://www.fufuok.com

/Chinese .rar-> automatic conversion of browser to-> http://www.fufuok.com/%D6%D0%CE%C4.rar
Note
Meaning: Firefox does not support the Chinese URL of GB2312 Encode, because it uses UTF-8 encoding to send URLs by default, but the ftp: // protocol is acceptable.
A bug in Firefox.

In an html file encoded as UTF-8:

Http://www.fufuok.com

/.Rar-> automatically convert the browser to->

Http://www.fufuok.com/%E4%B8%AD%E6%96%87.rar

Urlencode in PHP
:

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

Except -_.
All other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers.

Difference between urlencode and rawurlencode
:

Urlencode
Encode the space as the plus sign (+)
Rawurlencode encodes a space into a plus sign (% 20)

If you want to use the UTF-8 Encode, there are two types:
Method:

1. Save the file as a UTF-8 file and use urlencode and rawurlencode directly.
Ii. Use
Mb_convert_encoding function.

<? PHP
$ Url =
'Http: // www.fufuok.com/ .rar ';
Echo
Urlencode (mb_convert_encoding ($ URL, 'utf-8', 'gb2312 '). "/N ";
Echo
Rawurlencode (mb_convert_encoding ($ URL, 'utf-8', 'gb2312 '). "/N ";
// Http%3a%2f%2fwww.fufuok.com%2f%e4%b8%ad%e6%96%87.rar
?>

Ying
Instance

:

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: // Fufu: password@www.fufuok.com
/Chinese .rar ";
Echo parseurl ($ URL );
// Ftp: // Fufu: password@www.fufuok.com/% D6 % D0 % Ce % c4/127d6%d0%ce%c4.rar
?>

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.