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. jQuery Jcrop 图像裁剪

    jQuery Jcrop 图像裁剪 http://code.ciaoca.com/jquery/jcrop/ cropper.js 实现HTML5 裁剪图片并上传(裁剪上传头像.) https://b ...

  2. C# .Net计算函数执行的时间

    C#计算函数执行的时间 protected void StopwatchTest() { System.Diagnostics.Stopwatch stopwatch = new System.Dia ...

  3. ZH奶酪:基于ionic.io平台的ionic消息推送功能实现

    Hybrid App越来越火,Ionic的框架也逐渐被更多的人熟知. 在mobile app中,消息推送是很必要的一个功能. 国内很多ionic应用的推送都是用的极光推送,最近研究了一下Ionic自己 ...

  4. ZH奶酪:PHP 使用DOMDocument抓取网页

    原文链接:http://blog.csdn.net/xyzhaopeng/article/details/6626340 从一个HTML页面的一个表格中提取数据并且将这个数据整理出来加入到MySQL数 ...

  5. 微软BI 之SSRS 系列 - 如何实现报表标签的本地化 - 中文和英文的互换

    SSRS 中并没有直接提供本地化的配置方式,因此在 SSRS 中实现本地化,比如有英文标题还有可选的中文标题,就需要通过其它的方式来解决. 比如默认是这样的英文标题 - 但是本地中方用户可能比较喜欢看 ...

  6. 第六周 Word目录和索引

    第六周 Word目录和索引 教学时间 2013-4-2 教学课时 2 教案序号 5 教学目标 能正确使用索引.目录等 教学过程: 复习提问 1.脚注和尾注的区别是什么?2.如何插入脚注和尾注?3.如何 ...

  7. JAVA_SE基础——26.[深入解析]局部变量与成员变量的差别

    黑马程序猿入学blog ... 假设这章节非常难懂的话应该返回去先看  JAVA_SE基础--10.变量的作用域 定义的位置上差别: 1. 成员变量是定义在方法之外,类之内的. 2. 局部变量是定义在 ...

  8. J2EE框架知识清单

    1:Struts MVC.JVC 2:struts action 3:struts 1.0和2.0区别 4:Spring 核心机制:依赖注入 5:使用Spring容器 6:AOP的概念与应用 7:IO ...

  9. JavaWeb应用出现HTTP 500-Unable to compile class for JSP 错误 的解决

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6383192.html 在上一篇博文中,我们把自己本机的web项目部署到了云主机的tomcat上.之后通过浏览器 ...

  10. CentOS 7 安装php5.6,Nginx,Memcached环境及配置

    安装php5.6版本以后不再需要安装Zend Guard,而是用yum命令安装php-opcache及php-pecl-apcu就可以有效的提高php执行速度. 1. 配置yum源 事先确认yum源的 ...