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

yipeiwu_com6年前PHP代码库

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 & 'learn to use'<br />

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

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() 进行检测。

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实体。

预定义字符包括:& (和号) 成为&  
 " (双引号) 成为"
' (单引号) 成为'
< (小于) 成为<
> (大于) 成为>

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 & 'learn to use'<br />
just a test & 'learn to use'<br />
just a test & 'learn to use'

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

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

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

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

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 & 'learn to use '<br />

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

just a test & 'learn to use '

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

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

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

以上这篇浅谈htmlentities 、htmlspecialchars、addslashes的使用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

PHP超牛逼无限极分类生成树方法

你还在用浪费时间又浪费内存的递归遍历无限极分类吗,看了该篇文章,我觉得你应该换换了。 这是我在OSChina上看到的一段非常精简的PHP无限极分类生成树方法,巧在引用,整理分享了。 复制...

PHP实现删除多重数组对象属性并重新赋值的方法

本文实例讲述了PHP实现删除多重数组对象属性并重新赋值的方法。分享给大家供大家参考,具体如下: 实例:sphinx搜索出来的结果,要去掉某个属性值: $cl = new Sphinx...

整理php防注入和XSS攻击通用过滤

对网站发动XSS攻击的方式有很多种,仅仅使用php的一些内置过滤函数是对付不了的,即使你将filter_var,mysql_real_escape_string,htmlentities...

PHP设计模式之命令模式的深入解析

PHP设计模式之命令模式的深入解析

命令模式(Command),命令模式是封装一个通用操作的机制。 如果你熟悉C或PHP,你可能已经遇到过Command,它相当于程序中的:回调(callback)。回调通常使用一个函数指针...

phpmyadmin中禁止外网使用的方法

本文实例讲述了phpmyadmin中禁止外网使用的方法。分享给大家供大家参考。具体方法如下: 首先,在phpmyadmin文件夹中找到 phpmyadmin.conf 在文件中能看到如下...