登录  /  注册

如何将字符在PHP中的进行转义

一个新手
发布: 2017-09-06 17:03:40
原创
2675人浏览过

手动转义、还原字符

"\" 是一个转义符,紧跟在"\"后面的第一个字符将没有意义或有特殊意义。例如"'"是字符串的定界符,而"\'"中的"'"就失去了定界符的意义,变为了普通的单引号。

例如


echo 'select * from user where username=\'107lab\'';
登录后复制

运行结果为:

select * from user where username='107lab'
登录后复制

技巧:对于简单的字符串,建议采用手动的方法进行字符串转义,而对于数据量较大的字符串,建议采用自动转义函数实现字符串的转义。


自动转义,还原字符串

自动转义还原字符串可以应用PHP提供的addslashes()函数和stripslashes()函数实现。

addslashes()函数

用来给字符串str加入斜线"\",对指定字符串中的字符进行转义,该函数可以转义的字符包括,单引号,双引号,反斜杠,和NULL字符"0"。该函数常用于生成SQL语句时,对SQL语句中的部分字符进行转义。

语法如下:

string addslashes(string str);     参数str为将要被操作的字符串。
登录后复制

stripslashes()函数

用来将应用addslashes()函数转义后的字符str返回原样。

语法如下:

string stripslashes(string str);     参数str为将要被操作的字符串。
登录后复制
<span style="color:#6600; background-color:rgb(255,228,255)">$str</span><span style="background-color:rgb(247,250,255)">=</span><span style="color:#0800; background-color:rgb(247,250,255)"><strong>"select </strong></span><span style="color:#0800; background-color:rgb(247,250,255)"><em>*</em></span><span style="color:#0800; background-color:rgb(247,250,255)"><strong> from user where username=&#39;107lab&#39;"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="color:#6600; background-color:rgb(247,250,255)">$a</span><span style="background-color:rgb(247,250,255)">=</span><span style="background-color:rgb(247,250,255)"><em>addslashes</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="color:#6600; background-color:rgb(228,228,255)">$str</span><span style="background-color:rgb(247,250,255)">);<br/></span><span style="color:#0080; background-color:rgb(247,250,255)"><strong>echo </strong></span><span style="color:#6600; background-color:rgb(247,250,255)">$a</span><span style="background-color:rgb(247,250,255)">.</span><span style="color:#0800; background-color:rgb(247,250,255)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;<br/></span><span style="color:#6600; background-color:rgb(247,250,255)">$b</span><span style="background-color:rgb(247,250,255)">= </span><span style="background-color:rgb(247,250,255)"><em>stripslashes</em></span><span style="background-color:rgb(247,250,255)">(</span><span style="color:#6600; background-color:rgb(247,250,255)">$a</span><span style="background-color:rgb(247,250,255)">);<br/></span><span style="color:#0080; background-color:rgb(247,250,255)"><strong>echo </strong></span><span style="color:#6600; background-color:rgb(247,250,255)">$b</span><span style="background-color:rgb(247,250,255)">.</span><span style="color:#0800; background-color:rgb(247,250,255)"><strong>"<br>"</strong></span><span style="background-color:rgb(247,250,255)">;</span>
登录后复制

运行结果为:

select * from user where username=\'107lab\'
select * from user where username=&#39;107lab&#39;
登录后复制

注:所有数据在插入数据库之前,有必要应用addslashes()函数进行字符串转义,以免特殊字符未经转义在插入数据库时出现错误。

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

addcslashes()函数

实现对指定字符串进行转义,即在指定的字符前加上反斜杠。通过该函数可以将要添加到数据库中的字符串进行转义,从而避免出现乱码等问题。

语法如下

string addcslashes(string str,string charlist)
登录后复制

参数str为将要被操作的字符串;参数charlist指定在字符串中哪些字符前加上反斜杠"\",如果参数charlist中包含"\n"、"\r"等字符,将以C语言的风格转换,而其他非字母数字且ASCII码低于32以及高于126的字符均转换成八进制表示

stripcslashes()函数

用来将应用了addcslashes()函数转义的字符串str返回原样。

语法如下:

string stripcslashes(string str)
登录后复制

例如:

$str= addcslashes("107网站工作室","107网站工作室");
登录后复制
运行结果如下
登录后复制
\1\0\7\347\275\221\347\253\231\345\267\245\344\275\234\345\256\244
登录后复制

注:可以通俗的理解为在指定的字符前加上反斜杠

$str = addcslashes("A001 A002 A003","A");
echo($str);
登录后复制
运行结果如下
登录后复制
\A001 \A002 \A003
登录后复制

注释:addcslashes() 函数对大小写敏感。

注释:对以下字符应用 addcslashes() 时请小心:0(NULL), r(回车), n(换行), f 换页)、t(制表符)以及 v(垂直制表符)。在 PHP 中,\0, \r, \n, \t, \f 以及 \v 是预定义的转义序列。

以上就是如何将字符在PHP中的进行转义 的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号