建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > PHP教程 >

PHP 中的 addslashes 函数

时间:2010-02-09 22:27来源: 作者: 点击:
该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。 注释:默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这

addslashes() 函数在指定的预定义字符前添加反斜杠。换句话说就是字符转义,屏蔽掉特定字符。。。。

这些预定义字符是: 单引号 (')

双引号 (")

反斜杠 (\)

NULL

提示和注释

提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。 注释:默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。

例子

在本例中,我们要向字符串中的预定义添加反斜杠:

<?php $str = "Who's John Adams?";

echo $str . " This is not safe in a database query.";

echo addslashes($str) . " This is safe in a database query.";

?>

输出: Who's John Adams? This is not safe in a database query.

Who\'s John Adams? This is safe in a database query.

PHP官方介绍========

http://www.php.net/manual/en/function.addslashes.php

addslashes (PHP 4, PHP 5)

addslashes — Quote string with slashes string addslashes ( string $str )

Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte). An example use of addslashes() is when you're entering data into a database. For example, to insert the name O'reilly into a database, you will need to escape it. It's highly recommeneded to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL or pg_escape_string() for PostgreSQL), but if the DBMS you're using does't have an escape function and the DBMS uses \ to escape special chars, you can use this function. This would only be to get the data into the database, the extra \ will not be inserted. Having the PHP directive magic_quotes_sybase set to on will mean ' is instead escaped with another '. The PHP directive magic_quotes_gpc is on by default, and it essentially runs addslashes() on all GET, POST, and COOKIE data. Do not use addslashes() on strings that have already been escaped with magic_quotes_gpc as you'll then do double escaping. The function get_magic_quotes_gpc() may come in handy for checking this.

例子

<?php $str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?

echo addslashes($str);

?>

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片