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. C#.NET常见问题(FAQ)-如何把定义存放类实例的数组

    数组存放的可以是普通的int,double,string类型,也可以是自定义的类的实例   如果数组长度未知,可以用list对象存放   更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: h ...

  2. 查看一个目录是否已经mount --bind

    执行 mountpoint -q /test/mount echo $? 如果是0表示已经mount mountpoint -q /test/mount || mount -o bind /some/ ...

  3. 微信小程序 - wxpage

    WXPAGE 开源地址如下:https://github.com/tvfe/wxpage 极快的小程序打开 - 势必是用户体验的重中之重 #页面描述 A:代表全局App.js var wxpage = ...

  4. Multipathing for Software iSCSI

    see also:http://www.vmware.com/files/pdf/techpaper/vmware-multipathing-configuration-software-iSCSI- ...

  5. MySQL auto_increment初始值设置

    http://blog.csdn.net/u011439289/article/details/48055917 DROP TABLE IF EXISTS zan1; CREATE TABLE zan ...

  6. hadoop 文件合并

    来自:http://blog.csdn.net/dandingyy/article/details/7490046 众所周知,Hadoop对处理单个大文件比处理多个小文件更有效率,另外单个文件也非常占 ...

  7. python pdfplumber用于pdf表格提取

    import pdfplumber with pdfplumber.open('test.pdf') as pdf: #page_count = len(pdf.pages()) p0 = pdf.p ...

  8. C++ Standard Library

    C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...

  9. Webwork【04】Configuration 详解

    Webwork做为经典的Web MVC 框架,个人觉得源码中配置文件这部分代码的实现十分考究. 支持自定义自己的配置文件.自定义配置文件读取类.自定义国际化支持. 可以作为参考,单独引入到其他项目中, ...

  10. 《Unix&Linux大学教程》学习笔记二:指令常识

    1:指令的本质——调用可执行程序 在Shell输入命令时,其实就是根据名称运行相应的程序. 2:在系统中查找程序 which 指令名 type 指令名 3:时间与日历 date [-选项] 参数 ca ...