addslashes() 函數是 PHP 中的內建和預定義函數,用於傳回包含反斜線的字串,並在字串中存在的所有預定義字元之前加上前綴。 PHP的addslashes()函數有一個特殊的特點,它不會考慮從函數傳遞的任何參數或參數。預定義字元在此函數中的行為有所不同。與addcslashes() 函數相比,addslashes 函數略有不同,因為該函數接受需要在斜杠之前添加的指定字符,但對於addslashes() 函數來說並不重要,因為它不允許傳遞任何參數並在斜槓之前附加斜線指定的字元。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
addslashes($string)
- addslashes(): addslashes() 函數只允許從函數內部傳遞一個參數或參數,後面跟著字串的 $ 符號。
- $string : 這是將傳遞給addslashes 函數輸入為函數的參數。它指定指定字串的屬性的特殊結構。
傳回型別: 傳回型別是傳回在函數中加入字串的預定義字元前面有反斜線的字串。
addslashes() 函數在 PHP 中如何運作?
addslashes 是 PHP 定義的字串引用和字串包中的一個特殊的內建函數,它為帶斜槓的引號字串產生引號。它有一個傳回類型,傳回具有特殊格式的字串,字串中的字元將以字串為前綴:
- 單引號:用符號(‘)表示
- 雙引號:用符號(” “)表示
- 反斜線: 用符號 ( ) 表示
- Null: 由(NUL 位元組) 表示
用例將隨字串而變化,在帶有這些字元的字串之前添加前綴的字元將被轉義字元集。
此函數已在PHP 5.4.0 及更高版本後新增並開始使用,否則,在字串上使用的所有類型的GET、POST 和cookie 上,預設值將被視為magic_quotes_gpc addslashes() 函數。
有時,一旦與資料庫查詢合併,此函數就會表現異常。因此,在PHP使用addslashes函數時,需要根據需求類型和版本來檢查和應對。由於這使得使其與資料庫一起工作變得不明確,因此有時可能會出現使其與資料庫注入及其相關查詢同步的情況,因此非常需要為其建立特定於資料庫的轉義函數或準備好的語句使用。
此外,許多子字串充當addslashes函數或作為addslashes函數的一部分,該函數包括以下函數作為其同伴:
- stripcslashes():它是addslashes的一部分,用於產生一些不帶引號的字串,但用一些addcslashes引用。
- stripslashes():它也是addslashes函數的同伴,它取消引用任何產生的帶引號的字串。
- addcslashes():它是addslashes函數的另一部分,它與addslashes函數完全互補,它使用內建函數來引用字串,包括C風格模式。
- htmlspecialchars(): 此函數的工作方式是將特殊字元轉換為 HTML 實體。
- Quotemeta(): 此函數適用於稱為元字元類型的字元內的字元。
- get_magic_quotes_gpc():此函數的行為方式也類似,與 magic_quotes_gpc 設定的配置中的當前設定差異非常小。
PHP addslashes() 函數範例
以下是不同的範例:
範例#1
程式代表用於表示字串的addslashes函數,並輸出帶有轉義字元序列的產生字串。
代碼:
<?php $str = addslashes('hope you are doing good with educba!'); echo($str); ?>
輸出:
Example #2
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:
<?php $str = "Which car is Volkswaon?"; echo $str . " It Is safe during database injection.<br>"; echo addslashes($str) . " Safe for database injection."; ?>
Output:
Example #3
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:
<?php $str = addcslashes("Welcome Educba!","W"); echo($str); ?>
Output:
Example #4
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:
<?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>"; ?>
Output:
Example #5
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:
<?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'); ?>
Output:
Example #6
This program represents the HTML special characters with the elements to translate the element’s value into one value using string.
Code:
<?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>
Output:
Conclusion
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中文網其他相關文章!

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器