加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 编程开发 > PHP > 正文

浅谈htmlentities 、htmlspecialchars、addslashes的使用方法

发布时间:2021-01-17 04:12:03 所属栏目:PHP 来源:互联网
导读:下面小编就为大家带来一篇浅谈htmlentities 、htmlspecialchars、addslashes的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

echo html_entity_decode($str);

echo "
";

echo html_entity_decode($str,ENT_QUOTES);

echo "
";

echo html_entity_decode($str,ENT_NOQUOTES);

输出如下:

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

echo htmlentities($str,ENT_COMPAT);

echo "
";

echo htmlentities($str,ENT_QUOTES);

echo "
";

echo htmlentities($str,ENT_NOQUOTES);

输出如下:

查看源代码如下:

just a test & 'learn to use'

just a test & 'learn to use'

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

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

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

echoaddslashes($str3);

输出:

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

echo stripslashes($str4);

输出:

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

(大于) 成为>

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

echo htmlspecialchars($str5,ENT_COMPAT);

echo "
";

echo htmlspecialchars($str5,ENT_QUOTES);

echo "
";

echo htmlspecialchars($str5,ENT_NOQUOTES);

输出:

查看源代码:

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

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

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

" 成为 " (双引号) ' 成为 ' (单引号) < 成为 < (小于) > 成为 > (大于)

echo htmlspecialchars_decode($str6);

echo "
";

echo htmlspecialchars_decode($str6,ENT_QUOTES);

echo "
";

echo htmlspecialchars_decode($str6,ENT_NOQUOTES);

输出:

查看源代码:

just a test & 'learn to use '

just a test & 'learn to use '

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

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

以上这篇浅谈htmlentities 、htmlspecialchars、addslashes的使用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读