Home Backend Development PHP Tutorial Introduction to the difference between php urlencode and rawurlencode_PHP tutorial

Introduction to the difference between php urlencode and rawurlencode_PHP tutorial

Jul 13, 2016 pm 05:15 PM
php urlencode and introduce the difference and exist of

In PHP, both rawurlencode and rawurlencode encode characters. Let me introduce to you the difference between urlencode and rawurlencode. Friends who need to know can refer to it.

The purpose of urlencode is to encode a string and replace all non-alphanumeric characters except "-_" in the original string with a percent sign (%) followed by two hexadecimal digits. However, please note: For historical reasons, spaces will be replaced with + signs. Rawurlencode is actually the same as urlencode, and is also used to encode strings. The only difference is that it uses RFC1738 encoding, which means that spaces are replaced with %20.

Their corresponding decoding functions are urldecode and rawurldecode. Referring to the instructions on the official website, any %## and plus sign ('+') in the encoded string given by urldecode decoding are decoded into a space character; rawurldecode decodes the percent sign (%) in the character string followed by two Bit hexadecimal. There are two differences. First, urldecode will decode any two characters after the percent sign (%). For example, %MN will also be decoded, but it will fail; rawurldecode will only decode the percent sign (%). Only the last two characters in hexadecimal (0-9A-F) will be decoded. Second, urldecode will decode the + sign into a space.

Let’s take a look at the official introduction of the two functions in PHP.

urlencode — encode a URL string

Report a bug Description
string urlencode ( string $str )
This function makes it easy to encode a string and use it in the request part of the URL, and it also makes it easy to pass variables to the next page.

Report a bug parameter

str
The string to encode.

Report a bug return value
Returns a string in which all non-alphanumeric characters except -_. are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs (+). This encoding is the same as the encoding of WWW form POST data, and the same encoding as the application/x-www-form-urlencoded media type. For historical reasons, this encoding differs from the RFC1738 encoding (see rawurlencode()) in encoding spaces as plus signs (+).

string rawurlencode ( string $str ) Characters specified according to » RFC 3986 encoding.

The code is as follows
 代码如下 复制代码

Example #1 urlencode() 例子

echo '';
?>


Example #2 urlencode() 与 htmlentities() 例子

$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '
';
?>

Copy code

Example #1 urlencode() Example

echo '';
?>

Example #2 urlencode() and htmlentities() example


$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);

echo '';
?>

rawurlencode — Encode URLs according to RFC 1738
 代码如下 复制代码

Example #1 在 FTP URL 里包含一个密码

echo '';
?>
以上例程会输出:

Report a bug Description
Report a bug parameter str The URL to encode. Report a bug return value Returns a string in which all non-alphanumeric characters except -_. are replaced with a percent sign (%) followed by two hexadecimal digits. This is an encoding described in » RFC 3986, which is intended to protect literal characters from being interpreted as special URL delimiters, and to protect the URL format from being garbled by character conversions used by the transport medium (like some mail systems) . Example
The code is as follows Copy code
Example #1 Include a password in the FTP URL echo ''; ?> The above routine will output: From the above description of the decoding function, it can be inferred that anything encoded using urlencode or rawurlencode can be decoded using urldecode. However, if the original string contains spaces, the string encoded with urlencode will be obtained after decoding with rawurlencode. The string will be different from the original string.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628888.htmlTechArticleIn php, rawurlencode and rawurlencode both encode characters. Let me introduce urlencode and rawurlencode to you. Friends who need to know the difference can refer to it. The use of urlencode...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

See all articles