addslashes() 函數是 PHP 中的內建和預定義函數,用於傳回包含反斜線的字串,並在字串中存在的所有預定義字元之前加上前綴。 PHP的addslashes()函數有一個特殊的特點,它不會考慮從函數傳遞的任何參數或參數。預定義字元在此函數中的行為有所不同。與addcslashes() 函數相比,addslashes 函數略有不同,因為該函數接受需要在斜杠之前添加的指定字符,但對於addslashes() 函數來說並不重要,因為它不允許傳遞任何參數並在斜槓之前附加斜線指定的字元。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
addslashes($string)
傳回型別: 傳回型別是傳回在函數中加入字串的預定義字元前面有反斜線的字串。
addslashes 是 PHP 定義的字串引用和字串包中的一個特殊的內建函數,它為帶斜槓的引號字串產生引號。它有一個傳回類型,傳回具有特殊格式的字串,字串中的字元將以字串為前綴:
用例將隨字串而變化,在帶有這些字元的字串之前添加前綴的字元將被轉義字元集。
此函數已在PHP 5.4.0 及更高版本後新增並開始使用,否則,在字串上使用的所有類型的GET、POST 和cookie 上,預設值將被視為magic_quotes_gpc addslashes() 函數。
有時,一旦與資料庫查詢合併,此函數就會表現異常。因此,在PHP使用addslashes函數時,需要根據需求類型和版本來檢查和應對。由於這使得使其與資料庫一起工作變得不明確,因此有時可能會出現使其與資料庫注入及其相關查詢同步的情況,因此非常需要為其建立特定於資料庫的轉義函數或準備好的語句使用。
此外,許多子字串充當addslashes函數或作為addslashes函數的一部分,該函數包括以下函數作為其同伴:
以下是不同的範例:
程式代表用於表示字串的addslashes函數,並輸出帶有轉義字元序列的產生字串。
代碼:
<!DOCTYPE html> <html> <body> <?php $str = addslashes('hope you are doing good with educba!'); echo($str); ?> </body> </html>
輸出:
This program represents the addslashes function for the representation of the string with an output of the generated string with escaped sequences of characters but supporting the ambiguous behavior of the database injection.
Code:
<!DOCTYPE html> <html> <body> <?php $str = "Which car is Volkswaon?"; echo $str . " It Is safe during database injection.<br>"; echo addslashes($str) . " Safe for database injection."; ?> </body> </html>
Output:
This program represents the addcslashes() function for the representation of the string with an output of the generated string with escaped sequences of characters by adding a backslash in front of the letter W which behaves completely opposite of addslashes() function.
Code:
<!DOCTYPE html> <html> <body> <?php $str = addcslashes("Welcome Educba!","W"); echo($str); ?> </body> </html>
Output:
This program represents the addcslashes() function for the representation of the string with an output of the generated string with escaped sequences of characters by adding a backslash in front of the letter educba for adding backslashes which behave completely opposite of addslashes() function.
Code:
<!DOCTYPE html> <html> <body> <?php $str = "Thanks for revisiting the educba portal!"; echo $str."<br>"; echo addcslashes($str,'d')."<br>"; echo addcslashes($str,'c')."<br>"; echo addcslashes($str,'b')."<br>"; ?> </body> </html>
Output:
This program represents the addcslashes() function for the representation of the string with an output of the generated string with escaped sequences of characters by adding a backslash in front of the letter educba for adding multiple backslashes which behaves completely opposite of addslashes() function.
Code:
<!DOCTYPE html> <html> <body> <?php $str = "Thanks for revisiting to our portal of educba!"; echo $str."<br>"; echo addcslashes($str,'A..Z')."<br>"; echo addcslashes($str,'a..z')."<br>"; echo addcslashes($str,'a..g'); ?> </body> </html>
Output:
This program represents the HTML special characters with the elements to translate the element’s value into one value using string.
Code:
<!DOCTYPE html> <html> <body> <?php $str = "There are some good person."; echo htmlspecialchars($str); ?> <p>Translate into entities with HTML special characters with the elements to prevent browsers and prevent code running to display from input to the homepage</p> </body> </html>
Output:
The addslashes function is used to represent and traverse the string by prefixing the backslash string in front of the entire special character string and is used to quote the necessary and important string to be used simultaneously.
以上是PHP 加入斜線()的詳細內容。更多資訊請關注PHP中文網其他相關文章!