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

PHP multiple methods of filtering html tags_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:09:442263browse

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 = '/(|)|<.+?>/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<$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;
}

Filtering method three

function clear_html_label($html)
{
$search = array ("']*?>.*?'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