Home > Backend Development > PHP Tutorial > PHP addslashes() 函数

PHP addslashes() 函数

WBOY
Release: 2016-06-23 14:33:37
Original
972 people have browsed it

定义和用法

addslashes() 函数在指定的预定义字符前添加反斜杠。

这些预定义字符是:

单引号 (') 双引号 (") 反斜杠 (\) NULL 语法
addslashes(string)
Copy after login
提示和注释

提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。

注释:默认情况下,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.<br />";echo addslashes($str) . " This is safe in a database query.";?>输出:
Copy after login
Who's John Adams? This is not safe in a database query.Who\'s John Adams? This is safe in a database query.
Copy after login
 
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template