JS和PHP中URL编码转换:escape()、encodeURI()、encodeURIComponent()
JS和PHP中URL编码转换:escape()、encodeURI()、encodeURIComponent()
escape() 方法:
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
encodeURI() 方法:
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#
encodeURIComponent() 方法:
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。
encodeURIComponent() 函数 与 encodeURI() 函数的区别之处:
前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串),因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。
总结:
通过对三个函数的分析,我们可以知道:escape()除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。
而encodeURI() 用于编码整个URI,因为URI中的合法字符都不会被编码转换。encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的,它可以讲参数中的中文、特殊字符进行转义,而不会影响整个URL。
Here you can see how the various JavaScript and PHP functions apply to a range of common characters:
Input | JavaScript | PHP | ||||
escape | encodeURI | encodeURIComponent | urlencode | rawurlencode | htmlentities | |
---|---|---|---|---|---|---|
%20 | %20 | %20 | + | %20 | ||
! | %21 | ! | ! | %21 | %21 | ! |
@ | @ | @ | %40 | %40 | %40 | @ |
# | %23 | # | %23 | %23 | %23 | # |
$ | %24 | $ | %24 | %24 | %24 | $ |
% | %25 | %25 | %25 | %25 | %25 | % |
^ | %5E | %5E | %5E | %5E | %5E | ^ |
& | %26 | & | %26 | %26 | %26 | & |
* | * | * | * | %2A | %2A | * |
( | %28 | ( | ( | %28 | %28 | ( |
) | %29 | ) | ) | %29 | %29 | ) |
- | - | - | - | - | - | - |
_ | _ | _ | _ | _ | _ | _ |
= | %3D | = | %3D | %3D | %3D | = |
+ | + | + | %2B | %2B | %2B | + |
: | %3A | : | %3A | %3A | %3A | : |
; | %3B | ; | %3B | %3B | %3B; | ; |
. | . | . | . | . | . | . |
" | %22 | %22 | %22 | %22 | %22 | " |
' | %27 | ' | ' | %27 | %27 | ' |
\ | %5C | %5C | %5C | %5C | %5C | \ |
/ | / | / | %2F | %2F | %2F | / |
? | %3F | ? | %3F | %3F | %3F | ? |
%3C | %3C | %3C | %3C | %3C | < | |
> | %3E | %3E | %3E | %3E | %3E | > |
~ | %7E | ~ | ~ | %7E | %7E | ~ |
[ | %5B | %5B | %5B | %5B | %5B | [ |
] | %5D | %5D | %5D | %5D | %5D | ] |
{ | %7B | %7B | %7B | %7B | %7B | { |
} | %7D | %7D | %7D | %7D | %7D | } |
%60 | %60 | %60 | %60 | %60 |

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











PHP is a popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

As a front-end developer, understanding PHP is very necessary. Although PHP is a back-end development language, mastering a certain amount of PHP knowledge can help front-end developers better understand the entire web development process, improve work efficiency, and collaborate better with back-end developers. In this article, we will discuss some PHP-related knowledge that front-end developers need to know and provide specific code examples. What is PHP? PHP (HypertextPreprocessor) is a server

PHP, as a scripting language widely used in web development, has become one of the necessary skills for many Internet companies to recruit technical talents. However, for some learners who are just getting started or have a weak foundation, learning PHP may encounter some difficulties. How can you better improve your basic skills when facing the PHP written test? Next, we will introduce some learning methods, hoping to help everyone improve the basic knowledge and skills of PHP. 1. Develop the habit of reading PHP official documentation. PHP official documentation is a learning

In the process of learning PHP, beginners often encounter various errors. Although this is a natural process of learning, many beginners often lose patience because of improper handling of mistakes. This article will introduce basic PHP errors and solutions, aiming to help beginners get started with PHP more easily. 1. Syntax error 1.1 Missing semicolon In PHP, statements must end with a semicolon. If you accidentally omit a semicolon, an error will be reported. For example, the following code results in an error: <?phpecho"He

Array function is one of the most commonly used functions in PHP, which can be used to create, operate and manage arrays. When developing applications, using array functions can greatly improve development efficiency. This article will introduce some basic usage and examples of array functions in PHP to help everyone better understand and master array functions. 1. Creation and initialization of arrays Arrays in PHP can be created in the following ways: //Create arrays through the array() function $arr=array("a",&q

Overview of PHP values: To understand the important concepts of PHP, specific code examples are required. PHP (Hypertext Preprocessor) is a scripting language widely used in web development. It can be embedded in HTML or executed as a standalone script. In web development, it is important to understand some important concepts of PHP to write efficient and maintainable code. In this article, we will introduce some important concepts of PHP and provide specific code examples to help readers understand better. variable changes

PHP is a very popular open source server-side scripting language that is widely used in web development. To become a good PHP programmer, reading official documentation is essential. Whether you are a beginner or an experienced developer, these tips will help you read PHP documentation more effectively. Understand the document structure PHP official documentation is divided into multiple parts, including manuals, reference manuals, FAQs, extension library documents, etc. Before you start reading, understand the structure of the document and find the parts you need. Using the search function PHP documentation

In today's Internet era of information explosion, websites have become an important way for display and promotion in all walks of life, and PHP, as the most popular server-side scripting language, is undoubtedly one of the essential skills for many website developers. To become proficient in PHP, you first need to master several key knowledge points and deepen your understanding through specific code examples. 1. PHP basic syntax The basic syntax of PHP is similar to most programming languages, including variables, data types, operators, conditional statements, loop statements, etc. Here is a simple example
