Home Backend Development PHP Tutorial Notes on php character escaping

Notes on php character escaping

Jul 29, 2016 am 08:39 AM
escape

今天碰到一个处理文件特殊字符的事情,再次注意到这个问题.

在php中:

* 以单引号为定界符的php字符串,支持两个转义\'和\\

* 以双引号为定界符的php字符串,支持下列转义:

\n 换行(LF 或 ASCII 字符 0x0A(10))

\r 回车(CR 或 ASCII 字符 0x0D(13))

\t 水平制表符(HT 或 ASCII 字符 0x09(9))

\\ 反斜线

\$ 美元符号

\" 双引号

\[0-7]{1,3} 此正则表达式序列匹配一个用八进制符号表示的字符

\x[0-9A-Fa-f]{1,2} 此正则表达式序列匹配一个用十六进制符号表示的字符

举几个例子:

一个包含\0特殊字符的例子:

$str = "ffff\0ffff";
echo(strlen($str));
echo("\n");
for($i=0;$i<strlen($str);$i++)echo("\t".ord($str{$i}));
echo("\n");
Copy after login

输出结果:

----------------------

9

102 102 102 102 0 102 102 102 102

替换特殊字符的例子

$str = "ffff\0ffff";
$str = str_replace("\x0", "", $str);
//或者用$str = str_replace("\0", "", $str);
//或者用$str = str_replace(chr(0), "", $str);
echo(strlen($str));
echo("\n");
for($i=0;$i<strlen($str);$i++)echo("\t".ord($str{$i}));
echo("\n");
Copy after login

输出结果:

----------------------

8

102 102 102 102 102 102 102 102

八进制ascii码例子:

//注意,符合正则\[0-7]{1,3}的字符串,表示一个八进制的ascii码。

$str = "\0\01\02\3\7\10\011\08\8"; //这里的\8不符合要求,被修正为"\\8" (ascii为92和56)
echo(strlen($str));
echo("\n");
for($i=0;$i<strlen($str);$i++)echo("\t".ord($str{$i}));
echo("\n");
Copy after login

输出结果:

----------------------

11

0 1 2 3 7 8 9 0 56 92 56

十六进制ascii码例子:

$str = "\x0\x1\x2\x3\x7\x8\x9\x10\x11\xff";
echo(strlen($str));
echo("\n");
for($i=0;$i<strlen($str);$i++)echo("\t".ord($str{$i}));
echo("\n");
Copy after login

输出结果:

----------------------

10

0 1 2 3 7 8 9 16 17 255

【推荐阅读:php入门教程

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

Video Face Swap

Video Face Swap

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

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)

preg_quote() function in PHP: How to escape special characters in a string to regular expression characters preg_quote() function in PHP: How to escape special characters in a string to regular expression characters Nov 04, 2023 pm 02:15 PM

The preg_quote() function in PHP: How to escape special characters in a string into regular expression characters requires specific code examples. In development, we often use regular expressions to match and process strings. However, some strings may contain some special characters, such as metacharacters in regular expressions, which have special meanings and cause regular expressions to fail to work properly. In order to solve this problem, PHP provides the preg_quote() function to escape special characters in the string

Protective measures and security practices against XSS attacks Protective measures and security practices against XSS attacks Aug 10, 2023 pm 02:39 PM

Overview of protective measures and security practices against XSS attacks Cross-site scripting attacks (XSS) are a common security vulnerability that exploits a website's inadequate validation and filtering of user input. Attackers can steal users' sensitive information, such as login credentials, personal information, etc., by inserting malicious script code into web pages. In order to protect the security of our website and our users, we need to implement some protective measures and security practices to guard against this type of attack. Input validation and filtering First, we need to validate and filter user input to ensure that the entered data meets expectations

How to deal with the escaping problems of single quotes, double quotes and backslash errors in PHP language development? How to deal with the escaping problems of single quotes, double quotes and backslash errors in PHP language development? Jun 11, 2023 pm 05:33 PM

In PHP language development, single quotes, double quotes and backslashes are common characters, but when processing strings, they may cause escaping problems. Here's how to deal with these issues to ensure that your PHP code can handle these characters correctly. Single quotation mark problem In PHP, single quotation marks are used to represent strings, but using single quotation marks within single quotation marks will cause escaping problems. For example: $myString='I'maPHPdeveloper.'; In the above example, use backslash

Practical application of GO language string escaping and anti-escaping Practical application of GO language string escaping and anti-escaping Apr 07, 2024 pm 10:48 PM

Go language string escaping and anti-escaping allows developers to specify non-displayable characters or keep characters literal. Escape uses backslashes to convert special characters into escape sequences, while unescaping uses backticks to restore the original value of the escape sequence. Understanding these operations is crucial for working with text that contains special characters, such as escaping HTML tags into plain text without parsing them.

Commonly used regular expression escaping techniques in PHP programming Commonly used regular expression escaping techniques in PHP programming Mar 20, 2024 am 09:00 AM

Regular expression escaping techniques commonly used in PHP programming require specific code examples. Regular expressions are a very commonly used tool in PHP programming. Through regular expressions, we can quickly match, search, and replace operations in text. However, when using regular expressions, sometimes we encounter special characters that need to be escaped, otherwise unexpected matching results will occur. In this article, we will introduce regular expression escaping techniques commonly used in PHP programming and provide specific code examples. Escape slash `` in regular expressions

Use PHP function 'mysqli_real_escape_string' to escape special characters in strings to avoid SQL injection Use PHP function 'mysqli_real_escape_string' to escape special characters in strings to avoid SQL injection Jul 25, 2023 am 09:41 AM

Use the PHP function "mysqli_real_escape_string" to escape special characters in the string to avoid SQL injection Introduction: In the process of website development that interacts with the database, we often need to store the data entered by the user into the database. However, if the data entered by the user is not processed, a malicious user may use the entered data to conduct SQL injection attacks, causing serious damage to the database. To avoid this from happening, we can use the PHP function"

Understand the htmlspecialchars() function in PHP for escaping HTML special characters Understand the htmlspecialchars() function in PHP for escaping HTML special characters Nov 18, 2023 pm 04:19 PM

Understanding the htmlspecialchars() function in PHP is used to escape HTML special characters. The htmlspecialchars() function in PHP is a commonly used string processing function that is used to convert HTML special characters into corresponding entities. The purpose of this is to avoid syntax errors or security holes in HTML pages, and also to ensure that the data entered by the user is displayed correctly on the page. In many cases, we need to treat user-entered data as HTML code

Deciphering the secret of less than or equal to escape characters in MyBatis Deciphering the secret of less than or equal to escape characters in MyBatis Feb 21, 2024 pm 10:06 PM

Title: Deciphering the Mystery of Less Than or Equal Escape Characters in MyBatis When using MyBatis for database operations, you often encounter situations where you need to query for a value less than or equal to a certain value. In SQL statements, the less than or equal condition usually uses "

See all articles