Home  >  Article  >  Backend Development  >  php怎么过滤html标签?

php怎么过滤html标签?

PHPz
PHPzOriginal
2016-06-01 11:57:423575browse

php过滤html标签的方法:1、使用“strip_tags”函数剥去字符串中的HTML标签;2、使用“str_replace”方法;3、利用正则表达式来过滤html标签;4、使用“delhtml”方法过滤html标签。

php怎么过滤html标签?

php怎么过滤html标签?

方法1、使用strip_tags()函数剥去字符串中的 HTML 标签

strip_tags()函数剥去字符串中的 HTML、XML 以及 PHP 的标签。

<?php
echo strip_tags("Hello <b>world!</b>");
?>

输出:

Hello world!

方法2、字符串替换

str_replace(array(" ","&amp;nbsp;","\t","\r\n","\r","\n"),array("","","","","","")

方法3、利用正则表达式(效率稍微低一些)

$str=preg_replace("/<s*imgs+[^>]*?srcs*=s*(&#39;&#39;|")(.*?)\1[^>]*?/?s*>/i","", $str); //过滤img标签
 
$str=preg_replace("/s+/","", $str); //过滤多余回车
 
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
 
$str=preg_replace("/<!--.*?-->/si","",$str); //注释
 
$str=preg_replace("/<(!.*?)>/si","",$str); //过滤DOCTYPE
 
$str=preg_replace("/<(/?html.*?)>/si","",$str); //过滤html标签
 
$str=preg_replace("/<(/?head.*?)>/si","",$str); //过滤head标签
 
$str=preg_replace("/<(/?meta.*?)>/si","",$str); //过滤meta标签
 
$str=preg_replace("/<(/?body.*?)>/si","",$str); //过滤body标签
 
$str=preg_replace("/<(/?link.*?)>/si","",$str); //过滤link标签
 
$str=preg_replace("/<(/?form.*?)>/si","",$str); //过滤form标签
 
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
 
$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //过滤applet标签
 
$str=preg_replace("/<(/?applet.*?)>/si","",$str); //过滤applet标签
 
$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //过滤style标签
 
$str=preg_replace("/<(/?style.*?)>/si","",$str); //过滤style标签
 
$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //过滤title标签
 
$str=preg_replace("/<(/?title.*?)>/si","",$str); //过滤title标签
 
$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //过滤object标签
 
$str=preg_replace("/<(/?objec.*?)>/si","",$str); //过滤object标签
 
$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //过滤noframes标签
 
$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //过滤noframes标签
 
$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //过滤frame标签
 
$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //过滤frame标签
 
$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //过滤script标签
 
$str=preg_replace("/<(/?script.*?)>/si","",$str); //过滤script标签
 
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
 
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
 
$str=preg_replace("/on([a-z]+)s*=/si","On\1=",$str); //过滤script标签
 
$str=preg_replace("/&#/si","&#",$str); //过滤script标签

方法4:

function delhtml($str){  //清除html标签
$st=-1; //开始
$et=-1; //结束
$stmp=array();
$stmp[]=" ";
$len=strlen($str);
for($i=0;$i<$len;$i++){
  $ss=substr($str,$i,1);
  if(ord($ss)==60){ //ord("<")==60
  $st=$i;
  }
  if(ord($ss)==62){ //ord(">")==62
  $et=$i;
  if($st!=-1){
   $stmp[]=substr($str,$st,$et-$st+1);
  }
  }
}
$str=str_replace($stmp,"",$str);
return $str;
}

再来一个:

function clear_html_label($html) 
{ 
$search = array ("&#39;&#39;<script[^>]*?>.*?</script>&#39;&#39;si", "&#39;&#39;<[/!]*?[^<>]*?>&#39;&#39;si", "&#39;&#39;([rn])[s]+&#39;&#39;", "&#39;&#39;&(quot|#34);&#39;&#39;i", "&#39;&#39;&(amp|#38);&#39;&#39;i", "&#39;&#39;&(lt|#60);&#39;&#39;i", "&#39;&#39;&(gt|#62);&#39;&#39;i", "&#39;&#39;&(nbsp|#160);&#39;&#39;i", "&#39;&#39;&(iexcl|#161);&#39;&#39;i", "&#39;&#39;&(cent|#162);&#39;&#39;i", "&#39;&#39;&(pound|#163);&#39;&#39;i", "&#39;&#39;&(copy|#169);&#39;&#39;i", "&#39;&#39;&#(d+);&#39;&#39;e"); 
$replace = array ("", "", "1", """, "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");
return preg_replace($search, $replace, $html); 
}

以上几种方法均可以实现,不过各有优劣,小伙伴们根据自己的项目需求来选择吧。

更多相关知识,请访问 PHP中文网!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn