Home  >  Article  >  php教程  >  解析php addslashes()与addclashes()函数的区别和比较

解析php addslashes()与addclashes()函数的区别和比较

WBOY
WBOYOriginal
2016-06-13 11:45:481038browse

PHP addcslashes() 函数
定义和用法
addcslashes() 函数在指定的字符前添加反斜杠。
语法
addcslashes(string,characters)参数 描述
string 必需。规定要检查的字符串。
characters 可选。规定受 addcslashes() 影响的字符或字符范围。
提示和注释
注释:在对 0,r,n 和 t 应用 addcslashes() 时要小心。在 PHP 中,\0,\r,\n 和 \t 是预定义的转义序列。
实例
例子 1
在本例中,我们要向字符串中的特定字符添加反斜杠:
$str = "Hello, my name is John Adams.";
echo $str;
echo addcslashes($str,'m');
echo addcslashes($str,'J');
?>
输出:
Hello, my name is John Adams.
Hello, \my na\me is John Ada\ms.
Hello, my name is \John Adams.

而函数addslashes()的使用:
PHP addslashes() 函数
定义和用法
addslashes() 函数在指定的预定义字符前添加反斜杠。
这些预定义字符是:
•单引号 (')
•双引号 (")
•反斜杠 (\)
•NULL
语法
addslashes(string)参数 描述
string 必需。规定要检查的字符串。
提示和注释
提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。
注释:默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
例子
在本例中,我们要向字符串中的预定义字符添加反斜杠:
$str = "Who's John Adams?";
echo $str . " This is not safe in a database query.
";
echo addslashes($str) . " This is safe in a database query.";
?>
输出:
Who's John Adams? This is not safe in a database query.
Who\'s John Adams? This is safe in a database query.他们都有对应的去除他们添加的反斜杠的方法,分别是:stripcslashes()和stripslashes()。

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