addslashes()函数是PHP中的内置函数,它返回预定义字符前带有反斜杠的字符串。该参数中不包含任何指定的字符。
预定义的字符是:
单引号(’)
双引号(“)
反斜杠(\)
NULL
注意:addslashes()函数不同于addcslashes()函数接受要在其之前添加斜杠的指定字符,但是addslashes()函数在参数中不接受任何字符,而是在某些指定字符之前添加斜杠。
用法:
addslashes($string)
参数:addslashes()函数仅接受一个参数$string,该参数指定需要转义的输入字符串。我们也可以说此参数指定了一个字符串,我们要在其中在预定义字符之前添加反斜杠。
返回值:它返回在参数中传递的预定义字符前面带有反斜杠的转义字符串。
例子:
Input:$string = "Geek's"
Output:Geek\'s
Input:$string='twinkle loves "coding"'
Output:twinkle loves \"coding\"
以下示例程序旨在说明PHP中的addslashes()函数:
程序1:
// PHP program to demonstrate the
// working of addslashes() function
// Input String
$str = addslashes('twinkle loves "coding"');
// prints the escaped string
echo($str);
?>
输出:
twinkle loves \"coding\"
程序2:
// PHP program to demonstrate the
// working of addslashes() function
// Input String
$str = addslashes("Geek's");
// prints the escaped string
echo($str);
?>
输出:
Geek\'s