PHP Function details: urlencode ()

Source: Internet
Author: User
Tags alphanumeric characters
PHP Function details: urlencode ()

The urlencode function encodes the input string parameters. The returned string contains -. ", all non-alphanumeric characters are replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces are encoded as the plus sign (+ ). This function allows you to encode a string and use it in the request part of a URL. It also allows you to pass variables to the next page.

URLEncode: a pointer to the Chinese characters in the url of a web page. The most common method is to enter Chinese characters in search engines such as Baidu and Google, generate a webpage URL that has passed the Encode. URLEncode has two methods: the traditional GB2312-based Encode (Baidu, Yisou, etc.), and the UTF-8-based Encode (Google, Yahoo, etc ). 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 the html file encoded as GB2312, http://www.nowamagic.net/ .rar-> the browser automatically converts to-> http://www.nowamagic.net/%D6%D0%CE%C4.rar

Note: Firefox does not support the Chinese URL of the GB2312 Encode, because it is the UTF-8 encoding by default to send the URL, but ftp: // protocol can.

Encode the UTF-8 in the html file, http://www.nowamagic.net/ .rar-> the browser automatically converts to-> http://www.nowamagic.net/%E4%B8%AD%E6%96%87.rar

URLEncode in PHP:

  1. // GB2312 Encode
  2. Echo urlencode ("Chinese-_."). "\ n"; // % D6 % D0 % CE % C4-_. +
  3. Echo urldecode ("% D6 % D0 % CE % C4-_."). "\ n"; // Chinese -_.
  4. Echo rawurlencode ("Chinese-_."). "\ n"; // % D6 % D0 % CE % C4-_. % 20
  5. Echo rawurldecode ("% D6 % D0 % CE % C4-_."). "\ n"; // Chinese -_.
  6. ?>


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:

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

  1. $ Url = 'http: // www.nowamagic.net/ .rar ';
  2. Echo urlencode (mb_convert_encoding ($ url, 'utf-8', 'gb2312 '). "\ n ";
  3. Echo rawurlencode (mb_convert_encoding ($ url, 'utf-8', 'gb2312 '). "\ n ";
  4. // Http%3A%2F%2Fwww.nowamagic.net%2F%E4%B8%AD%E6%96%87.rar
  5. ?>


Instance:

  1. Function parseurl ($ url = "")
  2. {
  3. $ Url = rawurlencode (mb_convert_encoding ($ url, 'gb2312', 'utf-8 '));
  4. $ A = array ("% 3A", "% 2F", "% 40 ");
  5. $ B = array (":","/","@");
  6. $ Url = str_replace ($ a, $ B, $ url );
  7. Return $ url;
  8. }
  9. $ Url = "ftp: // ud03: password@www.nowamagic.net/Chinese/ .rar ";
  10. Echo parseurl ($ url );
  11. // Ftp: // ud03: password@www.nowamagic.net/% D6 % D0 % CE % C4/127d6%d0%ce%c4.rar
  12. ?>


URLEncode in JavaScript: % E4 % B8 % AD % E6 % 96% 87 -_. % 20% E4 % B8 % AD % E6 % 96% 87 -_. % 20, encodeURI does not encode the following characters: ":", "/", ";", "?" And.


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.