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是标准库中一种容器,相当于保存元素类 ...
随机推荐
- delphi跨平台SOCKET--System.Net.Socket
delphi跨平台SOCKET--System.Net.Socket 不知始于DELPHI哪一个版本,姑且始于柏林版吧. 基于此单元的TSocket类,大家可以很方便地封装出自己的服务端和客户端的SO ...
- XEvent--基础
--SQL Server 扩展事件具有高度可伸缩且高度可配置的体系结构,--使用户能够按需收集解决性能问题或确定性能问题所需的信息.--1. 性能损耗小--2. 可配置性高--3. 可捕获底层事件 - ...
- TSQL--自增键和索引对表的插入效率测试
问题描述:数据库操作中,需要对表频繁插入数据,但发现插入效率不高. 解决思路:将数据缓存在服务器上,积攒到一定条数后批处理发送到数据库,在插入时考虑并发和索引对插入效率的影响. 测试结果: ...
- Varnish 学习资料收集
高性能HTTP加速器Varnish(安装配置篇) 利用Varnish构建Cache服务器笔记 Varnish代理服务器部署 Varnish基础概念详解 Varnish的配置语言VCL及其内置变量介绍 ...
- linux bash变量替换(# ## % %% / //)
VAR=hahaha echo ${VAR#*h} # ahaha 从前向后匹配删除 VAR=hahaha echo ${VAR##*h} # a 贪婪模式,从前向后匹配删除所有 VAR=hahaha ...
- 上传文件时form表单需要添加的属性
enctype="multipart/form-data"
- Vsual Studio 高版本打开低版本项目的方法
https://www.cnblogs.com/jmliao/p/5594179.html
- asp.net 添加错误日志
在开发程序中,错误日志很有必要.今天就把使用到的添加错误日志,记录下来,方便以后查看 利用的asp.net错误处理机制 Application_Error 贴出代码 protected void Ap ...
- 解决vs2017调试出现脚本错误(/Community/Common7/IDE/PrivateAssemblies/plugin.vs.js) 方法
原文地址:http://bkcoding.cn/post_1204.html 新装的vs2017编译时出现当前页面脚本错误 url:/Community/Common7/IDE/PrivateAsse ...
- 【cocos2d-x 手游研发小技巧(4)与Android混编实现换“头像图片”】
cocos2dx在android平台上的游戏开发中往往会遇到一些混编需求,如: 比方有的社区类游戏需要用到更换玩家的“头像”操作,其实就是调用android servers服务里面的本地图片,以及选取 ...