PHP addslashes()函数用法详细示例

2021年4月11日15:45:02 发表评论 631 次浏览

addslashes()函数是PHP中的内置函数, 它返回预定义字符前带有反斜杠的字符串。该参数中不包含任何指定字符。

预定义的字符是:

  1. 单引号(’)
  2. 双引号(")
  3. 反斜杠(\)
  4. null

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

语法如下:

addslashes($string)

参数:addslashes()函数仅接受一个参数$字符串它指定需要转义的输入字符串。我们也可以说该参数指定了一个字符串, 我们要在其中在预定义字符之前添加反斜杠。

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

例子:

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


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: