當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP addslashes()用法及代碼示例

addslashes()函數是PHP中的內置函數,它返回預定義字符前帶有反斜杠的字符串。該參數中不包含任何指定的字符。

預定義的字符是:

  1. 單引號(’)
  2. 雙引號(“)
  3. 反斜杠(\)
  4. 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 
// 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  
// PHP program to demonstrate the 
// working of addslashes() function  
  
// Input String 
$str = addslashes("Geek's");  
  
// prints the escaped string 
echo($str);  
?>

輸出:

Geek\'s

參考:
http://php.net/manual/en/function.addslashes.php



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 PHP | addslashes() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。