PHP addcslashes 函式基本語法
String addcslashes( $string , $charlist )
語法中 addcslashes 函式的小括號內有兩個參數,第一個參數 $string 即為要轉換的字串,必填項目,第二個參數 $charlist 是用來設定哪些字符要跳脫,也就是會在這些設定的字符前面加上反斜線(\)的意思,可以設定獨特的字元符號或英文字母順序區段,大小寫有所差別。
PHP addcslashes 函式範例
<?php
$new_string = "Welcome to wibibi.It was a very nice day.";
echo $new_string.'<br>';
echo addcslashes($new_string,'W').'<br>';
echo addcslashes($new_string,'w').'<br>';
echo addcslashes($new_string,'A..Z').'<br>';
echo addcslashes($new_string,'a..z').'<br>';
echo addcslashes($new_string,'A..z').'<br>';
?>
以上輸出結果$new_string = "Welcome to wibibi.It was a very nice day.";
echo $new_string.'<br>';
echo addcslashes($new_string,'W').'<br>';
echo addcslashes($new_string,'w').'<br>';
echo addcslashes($new_string,'A..Z').'<br>';
echo addcslashes($new_string,'a..z').'<br>';
echo addcslashes($new_string,'A..z').'<br>';
?>
Welcome to wibibi.It was a very nice day.
\Welcome to wibibi.It was a very nice day.
Welcome to \wibibi.It \was a very nice day.
\Welcome to wibibi.\It was a very nice day.
W\e\l\c\o\m\e \t\o \w\i\b\i\b\i.I\t \w\a\s \a \v\e\r\y \n\i\c\e \d\a\y.
\W\e\l\c\o\m\e \t\o \w\i\b\i\b\i.\I\t \w\a\s \a \v\e\r\y \n\i\c\e \d\a\y.
範例第二次與第三次輸出的結果,主要在於呈現 addcslashes 函式對英文字母大小寫判斷,因為英文字母大小寫的 ASCII Code 並不相同,addcslashes 會自動分辨,接著後面三次的輸出則是呈現 addcslashes 函式對於英文字母順序的判斷,若 A..Z 是大寫,則字串 $new_string 中的所有大寫字母都會成為跳脫字元,相反的,若 a..z 是小寫,則跳脫字串中的小寫字元,若 A..z 這樣先大寫再小寫,則 $new_string 中的所有英文字母,無論大小寫,都會成為跳脫字元,其他符號如空白或頓號並不會有影響。\Welcome to wibibi.It was a very nice day.
Welcome to \wibibi.It \was a very nice day.
\Welcome to wibibi.\It was a very nice day.
W\e\l\c\o\m\e \t\o \w\i\b\i\b\i.I\t \w\a\s \a \v\e\r\y \n\i\c\e \d\a\y.
\W\e\l\c\o\m\e \t\o \w\i\b\i\b\i.\I\t \w\a\s \a \v\e\r\y \n\i\c\e \d\a\y.
PHP addcslashes 函式相關研究