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

php过滤html标签多种方法

May 25, 2016 pm 04:40 PM

方法一,代码如下:

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)