PHP addslashes() 函数
PHP 教程
· 2019-01-29 09:59:45
实例
在每个双引号(")前添加反斜杠:
<?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 教程 文章: https://codercto.com/courses/l/5.html
Pro Django
Marty Alchin / Apress / 2008-11-24 / USD 49.99
Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!