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>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool