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