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;  'learn to use'<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;
' (单引号) 成为'
< (小于) 成为&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; 'learn to use'<br />
                     just a test  &amp; 'learn to use'
6、 htmlspecialchars_decode():把一些预定义的html实体转换为字符。

会被解码的html实体包括:&amp; 成为 &(和号)
 &quot; 成为 " (双引号)
 ' 成为 ' (单引号)
 &lt; 成为 < (小于)
 &gt; 成为 > (大于)

Eg:$str6 = "just atest  &amp; '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 '

至此,我想大家对着几个函数的基本试用应经明白了吧

htmlentities、addslashes 、htmlspecialchars的使用的更多相关文章

  1. php中addslashes(),htmlspecialchars()

    参考转自http://czf2008700.blog.163.com/blog/static/2397283200937103250194/ addslashes -- 使用反斜线引用字符串 stri ...

  2. php过滤字段htmlentities,htmlspecialchars,strip_tags

    1.strip_tags:过滤html标签比如<a> <html> <script> 如: $str = '<a href="test.html&q ...

  3. 过滤输入htmlentities与htmlspecialchars用法

    过滤输入 (即来自所列数据源中的任何数据)是指,转义或删除不安全的字符.在数据到达应用的存储层之前,一定要过滤输入数据.这是第一道防线.假如网站的评论表单接收html,默认情况下 访客可以毫无阻拦地在 ...

  4. php htmlentities和htmlspecialchars 的区别

    很多人都以为htmlentities跟htmlspecialchars的功能是一样的,都是格式化html代码的,我以前也曾这么认为,但是今天我发现并不是这样的.   The translations ...

  5. 关于htmlentities 、htmlspecialchars、addslashes的使用

    1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '"; echo ht ...

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

    html_entity_decode():把html实体转换为字符. $str = "just atest & 'learn to use '"; echo html_en ...

  7. strip_tags、htmlentities、htmlspecialchars的区别

    一.strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签. strip_tags(string,allow) 注释:可通过allow设置允许的标签.这些标签不会被删除. ...

  8. PHP htmlentities 和 htmlspecialchars的区别

    一直对这两个转换htm字符为html实体的函数混淆不清,查询了一下文档,总结如下 htmlentities: Convert all applicable characters to HTML ent ...

  9. htmlspecialschars与htmlentities的区别

    根据php手册,htmlentities与htmlspecialchars功能几乎是一模一样.唯一的差别就是,对于无效的代码单元序列(通俗讲就是不认识的编码)是否进行编码.htmlentities会进 ...

  10. 153-PHP htmlentities函数

    <?php //定义一个HTML代码字符串 $str=<<<HTM <a href=#><b><i>到一个网址的链接</i>&l ...

随机推荐

  1. 使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar错误

    今天创建了一个maven项目,想使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the ...

  2. asp.net对象合并

    public class com { /// <summary> /// 把参数转为JSON字符串 /// </summary> /// <param name=&quo ...

  3. APP测试体系

    网上找的图片,总结的很好:

  4. windows 2012授权模型

    转自:http://www.aidanfinn.com/?p=13090 Remember that Microsoft licenses its servers at the physical le ...

  5. POSTGRESQL 创建表结构、修改字段、导入导出数据库(支持CSV)

    这两个月经常使用postgresql,总结一些经常使用的语句: --创建表 CREATE TABLE customers ( customerid SERIAL primary key , compa ...

  6. JAVA的Spring注入机制事例详解

    一.前言 最近使用Spring里面的依赖注入,比如StudentServiceImple2.java代码: package di.service.imple; import com.mengya.sp ...

  7. numpy 傅立叶得到幅值和频率

    做个备份,对 numpy 不熟,每次都找函数找半天. 代码里分几块: 1. 从 argc[1] 的文档中读取数据,并转化为 float.文档中有 2001 行,第一行为头,后面 2000 个是采样数据 ...

  8. 关于IOS某图片添加控件,图片从相册或拍照保存后,再次进入时点击放大图无法显示的问题

    某图片添加控件: https://github.com/XZTLLQ/LQPhotoPickerDemo 问题: 标题已说明 代码块: NSArray *alAssetUrl =(NSMutableA ...

  9. Java远程方法协议(JRMP)

    Java远程方法协议(英语:Java Remote Method Protocol,JRMP)是特定于Java技术的.用于查找和引用远程对象的协议.这是运行在Java远程方法调用(RMI)之下.TCP ...

  10. poi导出excel合并单元格(包括列合并、行合并)

    1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...