search
Homephp教程php手册php 字符转义 注意事项

php 字符转义 注意事项

Jun 13, 2016 pm 12:23 PM
phpbyandexistcharacterstringnewlinesupportPrecautionsofescape

在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;$iecho("\n");

输出结果:
----------------------

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;$iecho("\n");
输出结果:
----------------------
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;$iecho("\n");
输出结果:
----------------------
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;$iecho("\n");
输出结果:
----------------------
10
0 1 2 3 7 8 9 16 17 255

Statement
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

MantisBT

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment