zoukankan      html  css  js  c++  java
  • 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标记。

    我们看一个例子:

    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.   
    10. echo(strip_tags($str,100));  

    返回的结果是

    大小: 19.36 K
尺寸: 383 x 259
浏览: 13 次
点击打开新窗口浏览全图

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

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

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

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

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

    & (和) 转成 &amp; 
    " (双引号) 转成 &quot; 
    < (小于) 转成 &lt; 
    > (大于) 转成 &gt; 
     
    例子:
    PHP代码
    1. $str='  
    2. BlogGuy" />&&<"  
    3. ';   
    4.   
    5. 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.   
    5. echo(htmlentities($str));  

    返回结果

    大小: 21.81 K
尺寸: 257 x 206
浏览: 12 次
点击打开新窗口浏览全图

     选择的理由是什么呢?

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

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

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

    看看例子吧

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

    结果

    大小: 15.18 K
尺寸: 400 x 170
浏览: 17 次
点击打开新窗口浏览全图

  • 相关阅读:
    nginx使用vhost子目录
    nginx服务报错解决
    反向代理远端 单台tomcat 使用域名代理
    反向代理远端 单台tomcat 使用ip+端口
    nginx反向代理本地 两台web负载均衡 使用域名代理
    nginx反向代理本地 两台web负载均衡 使用ip+端口代理
    nginx反向代理本地 单台wed -使用域名代理
    nginx反向代理本地 单台wed -使用ip+端口代理
    php 在函数内引用全局变量 讲解引用
    Xdebug的安装与使用
  • 原文地址:https://www.cnblogs.com/doseoer/p/4244173.html
Copyright © 2011-2022 走看看