PHP | addslashes()函数

这个 addslashes() 函数是PHP中的一个内置函数,它返回一个在预定义字符前面带有反斜杠的字符串。它不接受参数中的任何指定字符。

null

预定义字符包括:

  1. 单引号(’)
  2. 双引号(“)
  3. 反斜杠()
  4. 无效的

注: addslashes()函数与 addcslashes() 函数接受要在其前面添加斜杠的指定字符,但addslashes()函数不接受参数中的任何字符,而是在某些指定字符之前添加斜杠。

语法:

addslashes($string)

参数: addslashes()函数只接受一个参数 $string 指定需要转义的输入字符串。我们还可以说,这个参数指定了一个字符串,我们希望在预定义字符之前添加反斜杠。

返回值: 它返回在参数中传递的预定义字符前面带有反斜杠的转义字符串。

例如:

Input : $string = "Geek's"
Output : Geek's

Input : $string='twinkle loves "coding"'
Output : twinkle loves "coding"

下面的程序演示了PHP中的addslashes()函数:

项目1:

<?php
// PHP program to demonstrate the
// working of addslashes() function
// Input String
$str = addslashes ( 'twinkle loves "coding"' );
// prints the escaped string
echo ( $str );
?>


输出:

twinkle loves "coding"

项目2:

<?php
// PHP program to demonstrate the
// working of addslashes() function
// Input String
$str = addslashes ( "Geek's" );
// prints the escaped string
echo ( $str );
?>


输出:

Geek's

参考 : http://php.net/manual/en/function.addslashes.php

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享