php的strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes解释
php函数蛮多的,要完整的每个函数都理解深刻是个挺有挑战性的事情。
strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes这几个函数我想就需要专门的强化一下。
第一个函数:strip_tags,去掉 HTML 及 PHP 的标记
注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。而本函数和 fgetss() 有着相同的功能。fgetss是从文件中读取文件,并去掉html和php标记。
我们看一个例子:
- $str='
- 博客小子,BlogGuy,wayswang" />
- <link rel="alternate" title="BlogGuy" href="rss.php" type="application/rss+xml" />
- <link rel="stylesheet" href="templates/default/style.css" type="text/css" media="all" />
- <link rel="stylesheet" href="include/code.css" type="text/css" media="all" />
- <script type="text/javascript">
- var postminchars = parseInt("
- ';
- echo(strip_tags($str,100));
返回的结果是

可见虽然去除了html代码,但是空格格式之类的并没有去掉。
网上找到一个函数,但是我个人不是很满意,权作记录。
- unction cutstr_html($string, $sublen)
- {
- $string = strip_tags($string);
- $string = preg_replace ('/\n/is', '', $string);
- $string = preg_replace ('/ | /is', '', $string);
- $string = preg_replace ('/ /is', '', $string);
- preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string);
- if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
- else $string = join('', array_slice($t_string[0], 0, $sublen));
- return $string;
- }
第二个函数:htmlspecialchars, 将特殊字元转成 HTML 格式
详细说本函数会转化一下字符
- $str='
- BlogGuy" />&&<"
- ';
- echo(htmlspecialchars($str));
BlogGuy" />&&<"
可见,本函数只转换以上4个字符,其他html标记是不转换的。所以康盛的uchome里面提供了另外一个函数可以选择
- //取消HTML代码
- function shtmlspecialchars($string) {
- if(is_array($string)) {
- foreach($string as $key => $val) {
- $string[$key] = shtmlspecialchars($val);
- }
- } else {
- $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
- str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));
- }
- return $string;
- }
第三个函数:htmlentities,将所有的字元都转成 HTML 字串
可能你还在遗憾htmlspecialchars只能处理4个html标记,现在你不要遗憾了,htmlentities是转化全部字符。不可无不强大,但是在我看来意义不大。
- $str='
- BlogGuy" />&&<##博客小子"
- ';
- echo(htmlentities($str));
返回结果

选择的理由是什么呢?
看源代码完全不知所云嘛!
第四个函数:stripslashes与addslashes本是一对,addslashes是使用反斜线引用字符串,stripslashes是还原addslashes引用的字符串。
该函数一般都是数据库查询之前就需要处理的必要步骤,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。
看看例子吧
- $str='
- BlogGuy" />&&<##博客小子\\\"
- ';
- echo(addslashes($str));
结果

php的strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes解释的更多相关文章
- strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结
一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...
- htmlentities,html_entity_decode,addslashes
PHP htmlspecialchars_decode() 函数 PHP htmlspecialchars() 函数 PHP html_entity_decode() 函数 PHP中混淆的三组函数总结 ...
- PHP的htmlspecialchars、strip_tags、addslashes解释
第一个函数:strip_tags,去掉 HTML 及 PHP 的标记 注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串.若是字串的 HTML 及 PHP 标签原来就有错,例如少了大 ...
- htmlentities、addslashes 、htmlspecialchars的使用
1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '";echo htm ...
- strip_tags、htmlentities、htmlspecialchars的区别
一.strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签. strip_tags(string,allow) 注释:可通过allow设置允许的标签.这些标签不会被删除. ...
- urlencode rawurlencode htmlspecialchars htmlentities
w string urlencode ( string $str ) 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此 ...
- php中htmlspecialchars()函数和addslashes()函数的使用和区别
在防止被注入攻击时,常会用到两个函数:htmlspecialchars()和addslashes()函数.这两个函数都是对特殊字符进行转义. 1)addslashes()作用及使用 addslashe ...
- php stripslashes() addslashes() 解析
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠. 实例: <?php $str = "Is your name O\'reilly?"; ...
- PHP内置的字符串处理函数
字符串的特点 1.其他类型的数据用在字符串类型处理函数中,会自动将其转化成字符串后,在处理 <?php echo substr("abcdefghijklmn",2,4 ...
随机推荐
- Entity Framework context per request
原文发布时间为:2011-09-24 -- 来源于本人的百度文章 [由搬家工具导入] http://www.blog.cyberkinetx.com/2011/05/15/entity-framewo ...
- linux多线程学习笔记六--一次性初始化和线程私有数据【转】
转自:http://blog.csdn.net/kkxgx/article/details/7513278 版权声明:本文为博主原创文章,未经博主允许不得转载. 一,一次性初始化 以保证线程在调用资源 ...
- Hrbust 2363 Symmys (Manacher + DP)
题目链接 Hrbust 2363 来源 “科林明伦杯”哈尔滨理工大学第七届程序设计团队赛 Problem J 题意 给出一个长度为$1e6$的字符串,求最小可重回文子串覆盖数量 首先Manach ...
- 余秋雨的话(与OI无关)
余秋雨的话 1.假如你想要一件东西,就放它走.它若能回来找你,就永远属于你:它若不回来,那根本就不是你的. 2. 一个人会落泪,是因为痛:一个人之所以痛,是因为在乎:一个人之所以在乎,是因为有感觉:一 ...
- AbstractAdvisingBeanPostProcessor---spring aop 处理器
开局一张图,我们先上张图 类的说明和继承关系/** * Base class for {@link BeanPostProcessor} implementations that apply a * ...
- MySQL复制表结构和内容到另一张表(转)
MySQL不要看它小,一个开源的产物,要学习它的东西真的很多.而它的一切是SQL Server无法比拟的. 复制表结构及数据到新表 create table 新表 select * from 旧表 只 ...
- Android 获取虚拟按键的高度
//获取虚拟按键的高度 public static int getNavigationBarHeight(Context context) { int result = 0; if (hasNavBa ...
- 在html里网页中嵌入优酷的视频
<html> <embed src="http://player.youku.com/player.php/sid/XMjAzOTk4NjI4/v.swf" qu ...
- MFC中 获取新输入编辑框的内容
//得到原始内容的长度 int len = m_editPoemFileStr.GetLength(); UpdateData(true); //得到新增加的内容 CString sNewStrin ...
- EasyMvc入门教程-高级控件说明(15)方位布局控件
现在很多管理后台都流行全屏切割的布局,大体结构如下图所示: 大家注意到没,整个布局是五个部分组成:“东西南北中”,EasyMvc对应的实现的代码为: @(Html.Q().Layout().TextC ...