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 ...
随机推荐
- bzoj 4131: 并行博弈 (parallel)
bzoj 4131: 并行博弈 (parallel) Description lyp和ld在一个n*m的棋盘上玩翻转棋,游戏棋盘坐标假设为(x, y),1 ≤ x ≤ n,1 ≤ y ≤ m,这个游戏 ...
- HDU 3792 素数打表
Description If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 ...
- Render 使用
Page页面文件,重新Render 方法,目的是把页面的ViewState信息放在最后,利于页面展示速度和SEO优化. Render方法对于重新Html控件还是很好用的. private static ...
- gridview行链接
原文发布时间为:2009-04-21 -- 来源于本人的百度文章 [由搬家工具导入] 点击行,链接!! 可这样,在GridView的RowDataBound输入代码,假如id在第0列,且不是摸板列: ...
- 极致 Web 性能 —— SPA 性能指南
前言 前端框架时代,为开发体验.效率与页面性能带来,非常大的革命.大家纷纷拿起一系列打包工具(webpack/parcel etc.),配合一系列加载器快速搭建起一个 SPA 页面. SPA 应用带来 ...
- 02深入理解C指针之---指针类型和值
该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 1.指针的类型: 可以在声明指针时,指定指针的类型,例如: (1)void *x 声 ...
- jquery 中的post和get方法同步问题
解决方法: 在需要同步的js代码前修改ajax的async属性. 有两种设置方法: 1: $.ajaxSettings.async = false; 2: $.ajaxSetup({ async : ...
- Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...
- 怎样在action中获得值栈ValueStack中的值
1,实现RequestAware接口 //模拟对象 User model=new User(); user.setName=“lisi”;2,ValueStack value=(Value ...
- luogu P1345 [USACO5.4]奶牛的电信Telecowmunication
https://www.luogu.org/problemnew/show/1345 拆点,中间建流量为1的边,跑最小割 #include<cstdio> #include<queu ...