首頁  >  文章  >  後端開發  >  如何將字元在PHP中的進行轉義

如何將字元在PHP中的進行轉義

一个新手
一个新手原創
2017-09-06 17:03:402838瀏覽

手动转义、还原字符

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

例如


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='107lab'"</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>"0c6dc11e160d3b678d68754cc175188a"</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>"0c6dc11e160d3b678d68754cc175188a"</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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn