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. docker toolbox在win7下的安装

    1.下载安装docker toolbox docker toolbox的下载地址: http://mirrors.aliyun.com/docker-toolbox/windows/docker-to ...

  2. Oracle 定时任务使用

    1:首先创建存储过程 create or replace procedure pro_rqsl_hmd is rsCursor sys_refcursor; rqslid ); nsrsbh ); h ...

  3. 移动端H5的一些基本知识点总结

    移动端H5的一些基本知识点总结 来到这家公司之后,和曾经的工作发生了非常大的转变.曾经我一直是做PC端页面的.来到如今这家公司之后,主要是做手机移动端的页面. 移动端的页面在我这个做习惯了PC端页面的 ...

  4. mysql的逻辑结构

    mysql 数据库的逻辑架构如下图: 第一层,即最上一层,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等. 第二层值得关注. ...

  5. React Native中常用ES6语法

    一:模块导入导出 //ES6 import React, { Component, PropTypes, } from 'react'; import { Image, Text } from 're ...

  6. DWR组件——基于远程过程调用实现Ajax

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6686115.html  一:DWR的用途 DWR(Direct Web Remoting)是一个Web远程调用 ...

  7. 【leetcode】solution in java——Easy1

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...

  8. sqrtx-开平方

    Implementint sqrt(int x). Compute and return the square root of x. 逐次逼近 class Solution { public: int ...

  9. java Map常用方法封装

      java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...

  10. 〖Linux〗在tmux同时使用bash和zsh

    个人有两份tmux配置文件: ~/.tmux.conf # 使用zsh,主要是日常使用,zsh太好使用了 ~/.tmux.conf.bash # 使用bash,主要是Android编译使用 按照tmu ...