Home > php教程 > php手册 > php 过滤html标签多种方法

php 过滤html标签多种方法

WBOY
Release: 2016-06-13 10:11:35
Original
877 people have browsed it

本文章收藏了多种关于php教程 过滤html标签方法的方法,有利用php自带函数过滤html标签也有使用用户自定义函数加正则表达式来过滤htm标签,好了下面我们先看二款最简单的方法吧。

方法一

echo strip_tags("hello world!");


strip_tags --- 去除字串中的html和php标签

语法 : string strip_tags (string str [, string allowable_tags])

说明 :

此函式试着从给予的字串中去除所有html和php标签,如果是不完整或是假的标签时则会有错误,它和fgetss( )使用相同的方法去除标签。

 

$reg = '/(?p>|)|<.>/i';
    echo preg_replace($reg,'$1',$str);
?>

过滤方法二

function delhtml($str){   //清除html标签
$st=-1; //开始
$et=-1; //结束
$stmp=array();
$stmp[]=" ";
$len=strlen($str);
for($i=0;$i    $ss=substr($str,$i,1);
   if(ord($ss)==60){ //ord("     $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 ("'<script>]*?>.*?</script>'si", "']*?>'si", "'([rn])[s]+'", "'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'(d+);'e");
$replace = array ("", "", "1", """, "&", "", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");

return preg_replace($search, $replace, $html);
}

//实例应用

$string ='aaa
<script>fdsafsa'; <br /> echo clear_html_label($string);//aaa fdsafsa</script>

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template