一、addslashes()函数
1.addslashes()函数,是在指定的预定字符前加反斜杠。语法:addslashes(str);
2.参数是一个字符串
3.这些预定义字符有四种,是:单引号(’),双引号(”)、反斜杠(\)和NULL
4.例如:
$str="Who's John Adams?";
echo $str."This is not safe in a database query.
";//输出:Who's John Adams?This is not safe in a database query.
echo addslashes($str)."This is safe in a database query.";//输出:Who\'s John Adams?This is sage in a database query.
?>
二、addcslashes()函数
1.addcslashes()函数,是在指定的字符前添加反斜杠。
语法:addcslashes(str,chararcters);
2.中参数str是必须的,规定要检查的字符串,而character是可选的,规定受addcslashes()影响的字符或字符范围。
3.例1:
$str="Hello,my name is John Adams.";
echo $str; //输出:Hello,my name is John Adams.
echo addcslashes($str,'m'); //输出:Hello,\my na\me is John Ada\ms.
echo addcslashes($str,'J'); //输出:Hello,my name is \John Adams.
?>
例2、
$str="Hello,my name is John Adams.";
echo $str; //输出:Hello,my name is John Adams.
echo addcslashes($str,'A..Z'); //输出:\Hello,my name is \John \Adams.
echo addcslashes($str,'a..z'); //输出:H\e\l\l\o,\m\y \n\a\m\e \i\s J\o\h\n A\d\a\m\s.
echo addcslashes($str,'a..h'); //输出:H\ello,my n\am\e is Jo\hn A\d\ams.
?>
注:addcslashes()函数,对于指定字符或者字符范围是区分大小写的。