PHP addslashes() 函数

PHP addslashes() 函数

PHP addslashes() 函数用于返回一个带有斜杠的引号字符串。它适用于以下一些字符:

  • 单引号 (')
  • 双引号 (")
  • 反斜杠 (\)
  • NUL(NUL字节)

语法

string addslashes ( string $str );
参数 描述 必需/可选
string String to be escaped 必需

示例1

<?php 
str = 'What does "WHO" mean?';
echo "Your string is :".str;
echo "<br>"."By using addslashes() function the result  is".addslashes($str); 
?> 

输出:

Your string is :What does "WHO" mean?
By using addslashes() function the result isWhat does \"WHO\" mean?

示例2

<?php
str = "Who's the father of PHP?";
echostr . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>

输出:

Who's the father of PHP? This is not safe in a database query.
Who\'s the father of PHP? This is safe in a database query.

示例3

<?php
str =  "Wow' PHP?";
eval("echo '" . addslashes(str) . "';");
?>

输出:

Wow' PHP?

addslashes()的用例是在一个字符串中转义上述字符,该字符串将由PHP进行评估:

示例4

<?php
str = "Is The Father of PHP'Rasmus?";
//Is The Father of PHP\'Rasmus?
echo addslashes(str);
?>

输出:

Is The Father of PHP\'Rasmus? 

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程