PHP之string之addslashes()函数使用
addslashes
- (PHP 4, PHP 5, PHP 7)
- addslashes — Quote string with slashes
- addslashes — 使用反斜线引用字符串
Description
string addslashes ( string $str )
// Returns a string with backslashes added before characters that need to be escaped. These characters are:
// 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符:
- single quote (') 单引号(')
- double quote (") 双引号(")
- backslash (\) 反斜线(\)
- NUL (the NUL byte) NUL(NULL 字符)
- A use case of addslashes() is escaping the aforementioned characters in a string that is to be evaluated by PHP:
- 一个使用 addslashes() 的例子是当你要往数据库中输入数据时。
<?php
$str = "O'Reilly?";
eval("echo '" . addslashes($str) . "';");
Prior to PHP 5.4.0, the PHP directive magic_quotes_gpc was on by default and it essentially ran addslashes() on all GET, POST and COOKIE data. addslashes() must not be used on strings that have already been escaped with magic_quotes_gpc, as the strings will be double escaped. get_magic_quotes_gpc() can be used to check if magic_quotes_gpc is on.
例如,将名字 O'reilly 插入到数据库中,这就需要对其进行转义。 强烈建议使用 DBMS 指定的转义函数 (比如 MySQL 是 mysqli_real_escape_string(),PostgreSQL 是 pg_escape_string()),但是如果你使用的 DBMS 没有一个转义函数,并且使用 \ 来转义特殊字符,你可以使用这个函数。 仅仅是为了获取插入数据库的数据,额外的 \ 并不会插入。 当 PHP 指令 magic_quotes_sybase 被设置成 on 时,意味着插入 ' 时将使用 ' 进行转义。
The addslashes() is sometimes incorrectly used to try to prevent SQL Injection. Instead, database-specific escaping functions and/or prepared statements should be used.
PHP 5.4 之前 PHP 指令 magic_quotes_gpc 默认是 on, 实际上所有的 GET、POST 和 COOKIE 数据都用被 addslashes() 了。 不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。 遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
Parameters
str
- The string to be escaped.
- 要转义的字符。
Return Values
- Returns the escaped string.
- 返回转义后的字符。
Examples
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/2/13
* Time: 下午7:41
*/
$str = 'just do it!';
// just do it!
echo addslashes( $str ) . PHP_EOL;
$str = "just do it!";
// just do it!
echo addslashes( $str ) . PHP_EOL;
$str = '\a\b\'';
// \\a\\b\'
echo addslashes( $str ) . PHP_EOL;
$str = '\\';
// \\
echo addslashes( $str ) . PHP_EOL;
See
All rights reserved
PHP之string之addslashes()函数使用的更多相关文章
- addslashes() 函数和stripslashes()函数
addslashes() 函数 定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 ...
- PHP addslashes() 函数
定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 addslashes(stri ...
- OC与c混编实现Java的String的hashcode()函数
首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...
- php中htmlspecialchars()函数和addslashes()函数的使用和区别
在防止被注入攻击时,常会用到两个函数:htmlspecialchars()和addslashes()函数.这两个函数都是对特殊字符进行转义. 1)addslashes()作用及使用 addslashe ...
- string类find函数返回值判定
string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
- c++中string的常用函数说明
string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...
- php 深入理解addslashes函数
php addslashes函数对于很多人来说并不陌生,但很大部分人只是了解皮毛,只知道addslashes函数是在特定字符前面加上反斜杠,本文章将带大家深入理解php addslashes函数的使用 ...
- C++ string类及其函数的讲解
文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...
随机推荐
- Sqler-Cmd
针对于sqler 工具cmd 部分 做了如下整理 1 Cluster Model 2 Regedit Model $servers= '192.168.25.xx','192.168.25.yy' W ...
- Solr相似度名词:VSM(Vector Space Model)向量空间模型
最近想学习下Lucene ,以前运行的Demo就感觉很神奇,什么原理呢,尤其是查找相似度最高的.最优的结果.索性就直接跳到这个问题看,很多资料都提到了VSM(Vector Space Model)即向 ...
- MVC中控制器向视图传值的四种方式
MVC中的控制器向视图传值有四种方式分别是 1 ViewDate 2.ViewBag 3.TempDate 4.Model 下面分别介绍四种传值方式 首先先显示出控制器中的代码 using S ...
- .netcore Swagger 生成 api接口文档
1, 引用第三方包, Swashbuckle.AspNetCore Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerUI 最简 ...
- NETCore调用AD域验证
一.添加引用 System.DirectoryServices System.DirectoryServices.AccountManagement 二.验证代码 声明域 string domainN ...
- PostMan工具使用之基础篇
PostMan工具使用之基础篇 一.什么是PostMan Postman一款非常流行的API调试工具.(其他测试工具 Jmeter.soapUI) 二.下载及安装: 1.下载: 下载地址:https: ...
- 部署网络存储ISCSI
1.什么是ISCSIInternet Small Computer System Interface 互联网小型计算机接口技术,是一种将SCS存储与以太网技术相结合,可以用来在互联网中传输SCSI接口 ...
- “全栈2019”Java第一百零八章:匿名内部类与final关键字
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 阿里云ros实例
模板文件 { "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { " ...
- 为IEnumerable类型添加Add方法
IEnumerable类型原生是没有Add方法的,你可以用Contact方法去为它添加元素, 1 items = items.Concat(new[] { "foo" }); 也可 ...