About the analysis of urlencode() URL encoding function in php

不言
Release: 2023-04-01 16:16:02
Original
2449 people have browsed it

This article mainly introduces the analysis of the urlencode() URL encoding function in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.

URLEncode: refers to the A method of encoding and converting Chinese characters in web page URLs. The most common method is to generate an encoded web page URL when a Chinese query is entered in search engines such as Baidu and Google.

There are generally two methods of URLEncode, 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:
中文-> Encode of GB2312 -> ����
中文-> Encode of UTF-8 -> Chinese
URLEncode in Html:
In the html file encoded as GB2312: http://s.php.cn/中文.rar -> The browser automatically converts to -> http://s.php.cn /����.rar
Note: Firefox does not support Chinese URLs of GB2312 Encode because it sends URLs in UTF-8 encoding by default, but the ftp:// protocol is OK. I tried it and it worked. I think this should be considered a Firefox bug.
In the html file encoded as UTF-8: http://s.php.cn/中文.rar -> The browser automatically converts to -> http://s.php.cn/中文.rar
URLEncode in PHP:

<?php 
//GB2312的Encode 
echo urlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.+ 
echo urldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_. 
echo rawurlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.%20 
echo rawurldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_. 
?>
Copy after login

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 " ", and rawurlencode encodes spaces as plus signs " ".
If you want to use UTF-8 Encode, there are two methods:
1. Save the file as a UTF-8 file and use urlencode or rawurlencode directly.
2. Use the mb_convert_encoding function:

<?php 
$url = &#39;http://s.php.cn/中文.rar&#39;; 
echo urlencode(mb_convert_encoding($url, &#39;utf-8&#39;, &#39;gb2312&#39;))."\n"; 
echo rawurlencode(mb_convert_encoding($url, &#39;utf-8&#39;, &#39;gb2312&#39;))."\n"; 
//http%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar 
?>
Copy after login

Example:

<?php 
function parseurl($url="") 
{ 
$url = rawurlencode(mb_convert_encoding($url, &#39;gb2312&#39;, &#39;utf-8&#39;)); 
$a = array("%3A", "%2F", "%40"); 
$b = array(":", "/", "@"); 
$url = str_replace($a, $b, $url); 
return $url; 
} 
$url="ftp://ud03:password@s.php.cn/中文/中文.rar"; 
echo parseurl($url); 
//ftp://ud03:password@s.php.cn/%D6%D0%CE%C4/%D6%D0%CE%C4.rar 
?>
Copy after login

URLEncode in JavaScript:
For example: Chinese-_. Chinese-_.
encodeURI is not correct for the following characters Encode: ":", "/", ";", "?", "@" and other special characters.
Such as: http://s.php.cn/中文.rarhttp://s.jb51.net/中文.rar

The above is the entire content of this article, I hope it will be helpful to everyone’s learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

PHP implements WeChat public platform enterprise account verification interface

About PHP function code for finding polynomial derivatives

The above is the detailed content of About the analysis of urlencode() URL encoding function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!