Home  >  Article  >  Backend Development  >  Detailed explanation of PHP functions: urlencode()

Detailed explanation of PHP functions: urlencode()

WBOY
WBOYOriginal
2016-07-25 08:46:411289browse
The urlencode function URL-encodes the incoming string parameters. Except for "ˉ—.", all non-alphanumeric characters in the returned string are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs (+). This function facilitates encoding the string and using it in the request part of the URL, and also facilitates passing variables to the next page.
URLEncode: refers to an encoding conversion method for Chinese characters in web page URLs. The most common one is that when Chinese queries are entered in search engines such as Baidu and Google, an encoded web page URL is generated. There are generally two methods of URLEncoding. One is the traditional Encode based on GB2312 (used by Baidu, Yisou, etc.), and the other is based on UTF-8 Encode (used by Google, Yahoo, etc.). This tool implements two methods of Encode and Decode respectively.
  • Chinese -> Encode of GB2312 -> %D6%D0%CE%C4
  • Chinese -> Encode of UTF-8 -> %E4%B8%AD%E6%96%87
    The URLEncode in
Html
is encoded into the GB2312 html file, http://www.nowamagic.net/中文.rar -> The browser automatically converts it to -> http://www.nowamagic. net/%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 Can.
In the html file encoded as UTF-8, 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. //Encode of GB2312
  2. echo urlencode ("Chinese -_. ")."n"; //%D6%D0%CE%C4-_.+
  3. echo urldecode("%D6%D0%CE%C4-_. ")."n"; // Chinese-_.
  4. echo rawurlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.%20
  5. echo rawurldecode("%D6%D0%CE%C4 -_. ")."n"; //Chinese-_.
  6. ?>
Copy code

All non-alphanumeric characters except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits.
The difference between urlencode and rawurlencode:
  • urlencode encodes spaces as plus signs (+)
  • rawurlencode encodes spaces as plus signs (%20)
If you want to use UTF-8 Encode, there are two methods:
  • Save the file as a UTF-8 file, just use urlencode or rawurlencode directly.
  • 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. ?>
Copy code

Example:
  1. function parseurl($url="")
  2. {
  3. $url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));
  4. $a = array("%3A", "%2F", "%40");
  5.           $b = array(":", "/", "@");                                                                                                  b, $url);
  6. return $url;
  7. }
  8. $url="ftp://ud03:password@www.nowamagic.net/中文/中文.rar";
  9. echo parseurl($url) ;
  10. //ftp://ud03:password@www.nowamagic.net/%D6%D0%CE%C4/%D6%D0%CE%C4.rar
  11. ?>
Copy code

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn