超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<addcslashesbin2hex>
Last updated: Fri, 22 Jun 2007

addslashes

(PHP 3, PHP 4 , PHP 5)

addslashes -- 使用反斜线引用字符串

描述

string addslashes ( string str)

返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

一个使用 addslashes() 的例子是当你要往数据库中输入数据时。例如,将名字 O'reilly 插入到数据库中,这就需要对其进行转义。大多数据库使用 \ 作为转义符:O\'reilly。这样可以将数据放入数据库中,而不会插入额外的 \。当 PHP 指令 magic_quotes_sybase 被设置成 on 时,意味着插入 ' 时将使用 ' 进行转义。

默认情况下,PHP 指令 magic_quotes_gpcon,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。

例子 1. addslashes() 示例

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

// 输出:Is your name O\'reilly?
echo addslashes($str);
?>

参见 stripslashes()htmlspecialchars()quotemeta()get_magic_quotes_gpc()




add a note add a note User Contributed Notes
addslashes
bruce dot j dot cadiz at boeing dot com
20-Nov-1999 01:22
In ref. to note stating
"Using an oracle database addslashes doesn`t work to escape single quotes."

This is not successful when passed to ora_parse():

http://www.php.net/manual/function.ora-parse.php3<p> Another way to deal with this problem is to use stripslashes():


http://www.php.net/manual/function.stripslashes.php3

function. PHP will try to "escape" single quotes on name
value pairs (POST or GET)

example: A query string is passed with single quotes like

(SELECT * FROM EMP WHERE EMPNAME = 'SMITH')


will be changed to

(SELECT * FROM EMP WHERE EMPNAME =\'SMITH\')


stripslashes():

http://www.php.net/manual/function.stripslashes.php3

will fix this.

(ora_parse($curs,stripslashes($query)));


<addcslashesbin2hex>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22