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是标准库中一种容器,相当于保存元素类 ...
随机推荐
- ORACLE PATCH 版本的查询 PL/SQL
--ORACLE PATCH 版本的查询 PL/SQL SELECT DD.PATCH_NAME, PP.CREATION_DATE, PP.DRIVER_FILE_NAM ...
- delphi http 403 获取不到服务器返回的错误消息 用浏览器打开url可以返回
用delphi的idhttp Get一个url如下: http://117.135.237.4:9090/agent/api/treatmentModeUpdate?userName=VDAwMIMQ ...
- Sqlite 快速插入数据到本地表中
用原始Insert方法太慢,网上找到了https://www.cnblogs.com/yisen-code/p/6897524.html 思路是: 开启事务,开启预处理,然后把SQL用参数传入具体值来 ...
- nancyfx的安装笔记
这个安装时很简单的 只要 Install-Package Nancy.Hosting.Aspnet 就行了. 需要注意的是,千万不要用那个模板安装,通过创建nancyfx类型项目的方式安装是有问题的. ...
- nancy中的身份验证
在nancy中,身份验证分为basic ,form ,token,stateless几种,basic和form这里不说了,其中如果是使用cookie来保存身份的, 需要注意的是:cookie有大小的限 ...
- JKS与P12证书互转
keytool -importkeystore -srckeystore cacerts -srcstoretype JKS -deststoretype PKCS12 -destkeystore k ...
- c# 检查报错详细
catch (DbEntityValidationException error) { string test = string.Empty; foreach (var validationError ...
- 关于Office软件中Word输入时卡顿无反应的解决办法!
最近在安装office2013时遇到了这样一个问题,就是在激活office之后,打开Word输入内容时十分卡顿.也是狂搜网上各种办法,有说改注册表的,也有说在office选项里设置什么输入法的,全试了 ...
- Devexpress GridControl无限高度惹得祸
异常提示: issue, place the grid into a container that will give a finite height to the grid, or manually ...
- ({i:0#.w|nt authority\iusr})Sharepoint impersonates the IUSR account and is denied access to resources
This hotfix makes a new application setting available in ASP.NET 2.0. The new application setting is ...