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 、htmlspecialchars、addslashes的使用的更多相关文章

  1. php的strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes解释

    php函数蛮多的,要完整的每个函数都理解深刻是个挺有挑战性的事情. strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes这几 ...

  2. htmlentities,html_entity_decode,addslashes

    PHP htmlspecialchars_decode() 函数 PHP htmlspecialchars() 函数 PHP html_entity_decode() 函数 PHP中混淆的三组函数总结 ...

  3. strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结

    一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...

  4. htmlentities、addslashes 、htmlspecialchars的使用

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

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

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

  6. php htmlentities和htmlspecialchars 的区别

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

  7. PHP对表单提交特殊字符的过滤和处理

    PHP关于表单提交特殊字符的处理方法做个汇总,主要涉及htmlspecialchars/addslashes/stripslashes/strip_tags/mysql_real_escape_str ...

  8. PHP安全编程

    转自:http://www.nowamagic.net/librarys/veda/detail/2076   1.关闭register_globals,以提高安全性 2.在部署环境,不要让不相关的人 ...

  9. 防XXS和SQL注入

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

  10. GeSHi Documentation

    GeSHi Documentation Version 1.0.8.11 Authors: © 2004 - 2007 Nigel McNie © 2007 - 2012 Benny Baumann ...

随机推荐

  1. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  2. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  3. 转: Android入门及效率开发

    评注: android第三方开源框架介绍不错 转:https://segmentfault.com/a/1190000004495351 入门 Android官方培训课程中文版:http://huka ...

  4. 转: ios app架构设计

    http://keeganlee.me/post/architecture/20160107 看完这一系列文章后就知道怎么回答这类问题了: App架构设计经验谈:接口的设计 App架构设计经验谈:技术 ...

  5. 真机调试时提示: could not change executable permissions on the application

    原因:同一个bundle identifier不能重复在两个app中使用: 解决:卸载掉手机上的另一个app即可重新安装.

  6. Jquery插件(CKEditor)

    描述 在html页面实现像word一样的编辑功能(可视化HTML编辑器) 解决方法 ckeditor插件官方网站 http://ckeditor.com/ 使用 1:去官方下载ckeditor插件,添 ...

  7. MySQL分区表的使用

    MySQL使用分区表的好处: 1,可以把一些归类的数据放在一个分区中,可以减少服务器检查数据的数量加快查询. 2,方便维护,通过删除分区来删除老的数据. 3,分区数据可以被分布到不同的物理位置,可以做 ...

  8. cocos2dx-lua之断点调试支持

    cocos2dx 3.2版对cocos code ide支持已经相当棒了,不过话说,编辑器用起来感觉没有sublime顺手 支持cocos code ide已经支持创建lua项目了,可是默认创建的项目 ...

  9. C# JSON 序列化和反序列化——JavaScriptSerializer实现

    一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...

  10. VxWorks 6.9 内核编程指导之读书笔记 -- C++开发

    5.1 介绍 针对C++的VxWorks配置 C++头文件 使用C++启动任务 C和C++之前调用代码 C++编译器说明 在信号处理和ISR中使用C++ 下载C++编写的内核模块 C++编译器的不同 ...