Home > php教程 > php手册 > body text

php 字符转义 注意事项

WBOY
Release: 2016-06-13 12:23:54
Original
980 people have browsed it

在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

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!