Home >Backend Development >PHP Tutorial >PHP multiple methods of filtering html tags_PHP tutorial

PHP multiple methods of filtering html tags_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:09:442409browse

This article collects a variety of methods about the php tutorial method of filtering html tags, including using PHP's own functions to filter html tags, and also using user-defined functions and regular expressions to filter html. Tag, okay, let’s look at the two simplest methods first.

Method 1

echo strip_tags("hello world!");


strip_tags --- Remove html and php tags from strings

Syntax: string strip_tags (string str [, string allowable_tags])

Description:

This function tries to remove all html and php tags from the given string. If it is an incomplete or false tag, an error will occur. It uses the same method as fgetss() to remove tags.

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

Filtering method two

function delhtml($str){ //Clear html tag
$st=-1; //Start
$et=-1; //End
$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;
}

Filtering method three

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);
}

//Example application

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629742.htmlTechArticleThis article collects a variety of methods on php tutorials to filter html tags, including using php's own functions to filter html Tags also use user-defined functions and regular expressions to filter HTML tags...
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