首頁  >  文章  >  後端開發  >  PHP 加入斜線()

PHP 加入斜線()

PHPz
PHPz原創
2024-08-29 12:54:44936瀏覽

addslashes() 函數是 PHP 中的內建和預定義函數,用於傳回包含反斜線的字串,並在字串中存在的所有預定義字元之前加上前綴。 PHP的addslashes()函數有一個特殊的特點,它不會考慮從函數傳遞的任何參數或參數。預定義字元在此函數中的行為有所不同。與addcslashes() 函數相比,addslashes 函數略有不同,因為該函數接受需要在斜杠之前添加的指定字符,但對於addslashes() 函數來說並不重要,因為它不允許傳遞任何參數並在斜槓之前附加斜線指定的字元。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

addslashes($string)
  • addslashes(): addslashes() 函數只允許從函數內部傳遞一個參數或參數,後面跟著字串的 $ 符號。
  • $string : 這是將傳遞給addslashes 函數輸入為函數的參數。它指定指定字串的屬性的特殊結構。

傳回型別: 傳回型別是傳回在函數中加入字串的預定義字元前面有反斜線的字串。

addslashes() 函數在 PHP 中如何運作?

addslashes 是 PHP 定義的字串引用和字串包中的一個特殊的內建函數,它為帶斜槓的引號字串產生引號。它有一個傳回類型,傳回具有特殊格式的字串,字串中的字元將以字串為前綴:

  1. 單引號:用符號(‘)表示
  2. 雙引號:用符號(” “)表示
  3. 反斜線: 用符號 ( ) 表示
  4. Null: 由(NUL 位元組)
  5. 表示

用例將隨字串而變化,在帶有這些字元的字串之前添加前綴的字元將被轉義字元集。

此函數已在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函數,並輸出帶有轉義字元序列的產生字串。

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
$str = addslashes('hope you are doing good with educba!');
echo($str);
?>
</body>
</html>

輸出:

PHP 加入斜線()

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:

<!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:

PHP 加入斜線()

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:

<!DOCTYPE html>
<html>
<body>
<?php
$str = addcslashes("Welcome Educba!","W");
echo($str);
?>
</body>
</html>

Output:

PHP 加入斜線()

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:

<!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:

PHP 加入斜線()

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:

<!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:

PHP 加入斜線()

Example #6

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:

PHP 加入斜線()

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 編輯 ()下一篇:PHP 編輯 ()