温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何正确的使用htmlentities 、htmlspecialchars和addslashes三者

发布时间:2020-12-22 14:13:27 来源:亿速云 阅读:162 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关如何正确的使用htmlentities 、htmlspecialchars和addslashes三者,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1、html_entity_decode():把html实体转换为字符。

Eg:$str = "just atest & 'learn to use '";

echo html_entity_decode($str);

echo "<br />";

echo html_entity_decode($str,ENT_QUOTES);

echo "<br />";

echo html_entity_decode($str,ENT_NOQUOTES);

输出如下:

just a test & 'learn to use '
just a test & 'learn to use '
just a test & 'learn to use '

2、htmlentities():把字符转换为html实体。

Eg:$str = "just a test & 'learn to use'";

 echo htmlentities($str,ENT_COMPAT);

 echo "<br/>";

 echo htmlentities($str, ENT_QUOTES);

 echo "<br/>";

 echo htmlentities($str, ENT_NOQUOTES);

输出如下:

just a test & 'learn to use'
just a test & 'learn to use'
just a test & 'learn to use'

查看源代码如下:

just a test &amp; 'learn to use'<br />

just a test &amp; &#039;learn to use&#039;<br />

just a test &amp; 'learn to use'

3、addslashes():在指定的预定义字符前添加反斜杠

预定义字符包括:单引号(‘),双引号(“),反斜杠(\),NULL

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

Eg:$str3="\ just a  '  \" test";

echoaddslashes($str3);

输出:

\\ just a \' \" test

4、stripslashes():删除由addslashes函数添加的反斜杠

Eg:$str4="\\ just a \'\" test";

echo stripslashes($str4);

输出:

just a ' " test

5、 htmlspecialchars():把一些预定义的字符转换为html实体。

预定义字符包括:& (和号) 成为&amp;  
 " (双引号) 成为&quot;
' (单引号) 成为&#039;
< (小于) 成为&lt;
> (大于) 成为&gt;

Eg:$str5 = "just atest & 'learn to use'";

echo htmlspecialchars($str5, ENT_COMPAT);

echo "<br/>";

echo htmlspecialchars($str5, ENT_QUOTES);

echo "<br/>";

echo htmlspecialchars($str5, ENT_NOQUOTES);

输出:

just a test & 'learn to use'
just a test & 'learn to use'
just a test & 'learn to use'

查看源代码: 

just a test &amp; 'learn to use'<br />
just a test &amp; &#039;learn to use&#039;<br />
just a test &amp; 'learn to use'

6、 htmlspecialchars_decode():把一些预定义的html实体转换为字符。

会被解码的html实体包括:&amp; 成为 &(和号)

 &quot; 成为 " (双引号)
 &#039; 成为 ' (单引号)
 &lt; 成为 < (小于)
 &gt; 成为 > (大于)

Eg:$str6 = "just atest &amp; &#039;learn to use&#039;";

echo htmlspecialchars_decode($str6);

echo "<br />";

echo htmlspecialchars_decode($str6, ENT_QUOTES);

echo "<br />";

echo htmlspecialchars_decode($str6, ENT_NOQUOTES);

输出:

just a test & 'learn to use '
just a test & 'learn to use '
just a test & 'learn to use '

查看源代码:

just a test & &#039;learn to use &#039;<br />

just a test & 'learn to use '<br />

just a test & &#039;learn to use &#039;

防注入防web脚本综合使用:

$str= htmlspecialchars(addslashes($str));

$str= htmlspecialchars_decode(stripslashes($str));

以上就是如何正确的使用htmlentities 、htmlspecialchars和addslashes三者,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI