首页  >  文章  >  后端开发  >  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 levenshtein()下一篇:PHP strtotime