Home > php教程 > php手册 > body text

php过滤html标签多种方法

WBOY
Release: 2016-05-25 16:40:43
Original
1483 people have browsed it

方法一,代码如下:

echo strip_tags("hello <b>world!</b>");
Copy after login

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

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

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

$reg = &#39;/(</?p>|<brs*/?>)|<.+?>/i&#39;;echo preg_replace($reg,&#39;$1&#39;,$str);
Copy after login

过滤方法二,代码如下:

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;//开源代码phprm.com 
       } 
       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; 
}
Copy after login

过滤方法三,代码如下:

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


Related labels:
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