PHP解决网址URL编码乱码问题
PHP解决网址URL编码乱码问题
在PHP中有urlencode()、urldecode()、rawurlencode()、rawurldecode()这些函数来解决网页URL编码解码问题。
PHP中urlencode介绍:
urlencode是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu、Google等搜索引擎中输入中文查询时候,生成经过 Encode过的网页URL。urlencode的方式一般有两种一种是传统的基于GB2312的Encode(Baidu、Yisou等使用),一种是基于utf-8的Encode(Google,Yahoo等使用)。本文分别分析两种方式的Encode与Decode。
中文 -> GB2312的Encode -> %D6%D0%CE%C4
中文 -> utf-8的Encode -> %E4%B8%AD%E6%96%87
Html中的urlencode:
编码为GB2312的html文件中:
http://www.scutephp.com/中文.rar -> 浏览器自动转换为 -> http://www.scutephp.com/%D6%D0%CE%C4.rar
注意:Firefox对GB2312的Encode的中文URL支持不好,因为它默认是utf-8编码发送URL的,但是ftp://协议可以,应该算是Firefox一个bug。
编码为utf-8的html文件中:
http://www.scutephp.com/中文.rar -> 浏览器自动转换为 -> http://www.scutephp.com/%E4%B8%AD%E6%96%87.rar
urlencode和rawurlencode的例子:
代码
<p>//GB2312的Encode</p><p>echo urlencode("中文-_. ")."\n"; <br /></p><p>//%D6%D0%CE%C4-_.+</p><p>echo urldecode("%D6%D0%CE%C4-_. ")."\n"; <br /></p><p>//中文-_.<br /></p><p>echo rawurlencode("中文-_. ")."\n";</p><p> //%D6%D0%CE%C4-_.%</p><p>05echo rawurldecode("%D6%D0%CE%C4-_. ")."\n"; <br /></p><p>//中文-_.</p>
除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数。
urlencode和rawurlencode的区别:
urlencode 将空格则编码为加号(+)
rawurlencode 将空格则编码为加号(%20)
我上个版本的txt文件分割器(在线)代码都是采用urlencode,从来没有发现过这个问题,结果导致今天出了严重的bug,所有带空格的url都无法解析了,导致分割好的文件无法下载。使用rawurlencode()函数,解决了这个问题。
如果要使用utf-8的Encode,有两种方法:
一、将文件存为utf-8文件,直接使用urlencode、rawurlencode即可。
二、使用mb_convert_encoding函数。
代码
<p>$url = 'http://www.phpernote.com/中文.rar';</p><p>echo urlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."\n";</p><p>echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."\n";</p><p>//http%3A%2F%2Fwww.huikaiche.com%2F%E4%B8%AD%E6%96%87.rar</p>
应用实例:
代码
<p>function parseurl($url=""){</p><p> $url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));</p><p> $a = array("%3A", "%2F", "%40");</p><p> $b = array(":", "/", "@");</p><p> $url = str_replace($a, $b, $url);</p><p> return $url;</p><p>}</p><p>$url="ftp://yongfu:password@www.huikaiche.com/中文/中文.rar";</p><p>echo parseurl($url);</p><p>//ftp://yongfu:password@www.huikaiche.com/%D6%D0%CE%C4/%D6%D0%CE%C4.rar</p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
