- PHP 正则表达式(PCRE)
- PHP 图像处理
- PHP 5 Array 函数
- PHP 5 Calendar 函数
- PHP cURL 函数
- PHP 5 Date/Time 函数
- PHP 5 Directory 函数
- PHP Error 和 Logging 函数
- PHP 5 Filesystem 函数
- PHP Filter 函数
- PHP FTP 函数
- PHP HTTP 函数
- PHP Libxml 函数
- PHP Mail 函数
- PHP 5 Math 函数
- PHP 杂项 函数
- PHP 5 MySQLi 函数
- PHP 5 SimpleXML 函数
- PHP lcfirst() 函数
- PHP 5 String 函数
- PHP XML Parser 函数
- PHP Zip File 函数
- PHP 5 时区
PHP addslashes() 函数
实例
在每个双引号(")前添加反斜杠:
<?php
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
定义和用法
addslashes() 函数返回在预定义的字符前添加反斜杠的字符串。
预定义字符是:
- 单引号(')
- 双引号(")
- 反斜杠(\)
- NULL
提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。
注释:默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
语法
addslashes(string)
参数 | 描述 |
---|---|
string | 必需。规定要转义的字符串。 |
技术细节
返回值: | 返回已转义的字符串。 |
---|---|
PHP 版本: | 4+ |
更多实例
实例 1
向字符串中的预定义字符添加反斜杠:
<?php
$str = "Who's Peter Griffin?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>
$str = "Who's Peter Griffin?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>
PHP String 参考手册
下一篇:PHP bin2hex() 函数
关注微信小程序
扫描二维码
程序员编程王