一、addslashes()函數
1.addslashes()函數,是在指定的預定字元前加反斜線。文法:addslashes(str);
2.參數是一個字串
3.這些預定義字元有四種,是:單引號(’),雙引號(”)、反斜線(\)和NULL
4.例如:
<?php $str="Who's John Adams?"; echo $str."This is not safe in a database query.<br/>";//輸出: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. ?>
12345678910
二、addcslashes()函數
1.addcslashes()函數,是在指定的字元前添加反斜線。
文法:addcslashes(str,chararcters);
2.中參數str是必須的,規定要檢查的字串,而character是可選的,規定受addcslashes()影響的字元或字元範圍。
3.例1:
<?php $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 ?>
123456789101112
例2、
<?php$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.?>
1234567891011121314
註:addcslashes()函數,對於指定字元或者字元範圍是區分大小寫。
在字元 "W" 前添加反斜線:
<?php $str = addcslashes("Hello World!","W");echo($str); ?>
定義和用法
addcslashes() 函數返回在指定的字元前添加反斜線的字串。
注釋:addcslashes() 函數是區分大小寫。
注釋:在對 0(NULL)、r(斷行符號)、n(換行)、t(換頁)、f(定位字元)和 v(垂直定位字元)應用 addcslashes() 時要小心。在 PHP 中,\0、\r、\n、\t、\f 和 \v 是預定義的逸出序列。
文法
addcslashes(string,characters)
參數 描述
string 必需。規定要轉義的字串。
characters 必需。規定要轉義的字元或字元範圍。
技術細節 傳回值:
返回已轉義的字串。
向字串中的特定字元添加反斜線:
<?php$str = "Welcome to my humble Homepage!";echo $str."<br>";echo addcslashes($str,'m')."<br>";echo addcslashes($str,'H')."<br>";?>
執行個體 2
向字串中的一個範圍內的字元添加反斜線:
<?php$str = "Welcome to my humble Homepage!";echo $str."<br>";echo addcslashes($str,'A..Z')."<br>";echo addcslashes($str,'a..z')."<br>";echo addcslashes($str,'a..g');?>