php函数蛮多的,要完整的每个函数都理解深刻是个挺有挑战性的事情。

strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes这几个函数我想就需要专门的强化一下。

第一个函数:strip_tags,去掉 HTML 及 PHP 的标记

注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。而本函数和 fgetss() 有着相同的功能。fgetss是从文件中读取文件,并去掉html和php标记。

我们看一个例子:

PHP代码
  1. $str='
  2. 博客小子,BlogGuy,wayswang" />
  3. <link rel="alternate" title="BlogGuy" href="rss.php" type="application/rss+xml" />
  4. <link rel="stylesheet" href="templates/default/style.css" type="text/css" media="all" />
  5. <link rel="stylesheet" href="include/code.css" type="text/css" media="all" />
  6. <script type="text/javascript">
  7. var postminchars = parseInt("
  8. ';
  9. echo(strip_tags($str,100));

返回的结果是

可见虽然去除了html代码,但是空格格式之类的并没有去掉。

网上找到一个函数,但是我个人不是很满意,权作记录。

PHP代码
  1. unction cutstr_html($string, $sublen)
  2. {
  3. $string = strip_tags($string);
  4. $string = preg_replace ('/\n/is', '', $string);
  5. $string = preg_replace ('/ | /is', '', $string);
  6. $string = preg_replace ('/ /is', '', $string);
  7. 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);
  8. if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
  9. else $string = join('', array_slice($t_string[0], 0, $sublen));
  10. return $string;
  11. }

第二个函数:htmlspecialchars, 将特殊字元转成 HTML 格式

详细说本函数会转化一下字符

& (和) 转成 &amp; 
" (双引号) 转成 &quot; 
< (小于) 转成 &lt; 
> (大于) 转成 &gt; 
 
例子:
PHP代码
  1. $str='
  2. BlogGuy" />&&<"
  3. ';
  4. echo(htmlspecialchars($str));
返回结果:

BlogGuy&quot; /&gt;&amp;&amp;&lt;&quot;
可见,本函数只转换以上4个字符,其他html标记是不转换的。所以康盛的uchome里面提供了另外一个函数可以选择
PHP代码
  1. //取消HTML代码
  2. function shtmlspecialchars($string) {
  3. if(is_array($string)) {
  4. foreach($string as $key => $val) {
  5. $string[$key] = shtmlspecialchars($val);
  6. }
  7. } else {
  8. $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
  9. str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));
  10. }
  11. return $string;
  12. }

第三个函数:htmlentities,将所有的字元都转成 HTML 字串

可能你还在遗憾htmlspecialchars只能处理4个html标记,现在你不要遗憾了,htmlentities是转化全部字符。不可无不强大,但是在我看来意义不大。

PHP代码
  1. $str='
  2. BlogGuy" />&&<##博客小子"
  3. ';
  4. echo(htmlentities($str));

返回结果

选择的理由是什么呢?

看源代码完全不知所云嘛!

第四个函数:stripslashes与addslashes本是一对,addslashes是使用反斜线引用字符串,stripslashes是还原addslashes引用的字符串。

该函数一般都是数据库查询之前就需要处理的必要步骤,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

看看例子吧

PHP代码
  1. $str='
  2. BlogGuy" />&&<##博客小子\\\"
  3. ';
  4. echo(addslashes($str));

结果

php的strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes解释的更多相关文章

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

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

  2. htmlentities,html_entity_decode,addslashes

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

  3. PHP的htmlspecialchars、strip_tags、addslashes解释

    第一个函数:strip_tags,去掉 HTML 及 PHP 的标记 注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串.若是字串的 HTML 及 PHP 标签原来就有错,例如少了大 ...

  4. htmlentities、addslashes 、htmlspecialchars的使用

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

  5. strip_tags、htmlentities、htmlspecialchars的区别

    一.strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签. strip_tags(string,allow) 注释:可通过allow设置允许的标签.这些标签不会被删除. ...

  6. urlencode rawurlencode htmlspecialchars htmlentities

    w string urlencode ( string $str ) 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此 ...

  7. php中htmlspecialchars()函数和addslashes()函数的使用和区别

    在防止被注入攻击时,常会用到两个函数:htmlspecialchars()和addslashes()函数.这两个函数都是对特殊字符进行转义. 1)addslashes()作用及使用 addslashe ...

  8. php stripslashes() addslashes() 解析

    stripslashes() 函数删除由 addslashes() 函数添加的反斜杠. 实例: <?php $str = "Is your name O\'reilly?"; ...

  9. PHP内置的字符串处理函数

    字符串的特点    1.其他类型的数据用在字符串类型处理函数中,会自动将其转化成字符串后,在处理 <?php echo substr("abcdefghijklmn",2,4 ...

随机推荐

  1. bzoj 4131: 并行博弈 (parallel)

    bzoj 4131: 并行博弈 (parallel) Description lyp和ld在一个n*m的棋盘上玩翻转棋,游戏棋盘坐标假设为(x, y),1 ≤ x ≤ n,1 ≤ y ≤ m,这个游戏 ...

  2. 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 ...

  3. Render 使用

    Page页面文件,重新Render 方法,目的是把页面的ViewState信息放在最后,利于页面展示速度和SEO优化. Render方法对于重新Html控件还是很好用的. private static ...

  4. gridview行链接

    原文发布时间为:2009-04-21 -- 来源于本人的百度文章 [由搬家工具导入] 点击行,链接!! 可这样,在GridView的RowDataBound输入代码,假如id在第0列,且不是摸板列: ...

  5. 极致 Web 性能 —— SPA 性能指南

    前言 前端框架时代,为开发体验.效率与页面性能带来,非常大的革命.大家纷纷拿起一系列打包工具(webpack/parcel etc.),配合一系列加载器快速搭建起一个 SPA 页面. SPA 应用带来 ...

  6. 02深入理解C指针之---指针类型和值

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 1.指针的类型: 可以在声明指针时,指定指针的类型,例如: (1)void *x  声 ...

  7. jquery 中的post和get方法同步问题

    解决方法: 在需要同步的js代码前修改ajax的async属性. 有两种设置方法: 1: $.ajaxSettings.async = false; 2: $.ajaxSetup({ async : ...

  8. 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 ...

  9. 怎样在action中获得值栈ValueStack中的值

    1,实现RequestAware接口 //模拟对象    User model=new User();    user.setName=“lisi”;2,ValueStack value=(Value ...

  10. luogu P1345 [USACO5.4]奶牛的电信Telecowmunication

    https://www.luogu.org/problemnew/show/1345 拆点,中间建流量为1的边,跑最小割 #include<cstdio> #include<queu ...