In the process of doing projects, we often need to filter some HTML tags to improve data security. In fact, it is to delete those Data that is potentially harmful to the application. It is used to strip tags and remove or encode unwanted characters.
First share some common ones
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
$str=preg_replace("/
$str=preg_replace("/s /","", $str); //Filter redundant carriage returns
$str=preg_replace("/<[ ] /si","<",$str); //Filter <__("<" with a space after it)
$str=preg_replace("//si","",$str); //Comments
$str=preg_replace("/<(!.*?)>/si","",$str); //Filter DOCTYPE
$str=preg_replace("/<(/?html.*?)>/si","",$str); //Filter html tags
$str=preg_replace("/<(/?head.*?)>/si","",$str); //Filter head tag
$str=preg_replace("/<(/?meta.*?)>/si","",$str); //Filter meta tags
$str=preg_replace("/<(/?body.*?)>/si","",$str); //Filter body tag
$str=preg_replace("/<(/?link.*?)>/si","",$str); //Filter link tags
$str=preg_replace("/<(/?form.*?)>/si","",$str); //Filter form tags
$str=preg_replace("/cookie/si","COOKIE",$str); //Filter COOKIE tags
$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //Filter applet tag
$str=preg_replace("/<(/?applet.*?)>/si","",$str); //Filter applet tags
$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //Filter style tag
$str=preg_replace("/<(/?style.*?)>/si","",$str); //Filter style tag
$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //Filter title tag
$str=preg_replace("/<(/?title.*?)>/si","",$str); //Filter title tag
$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //Filter object tag
$str=preg_replace("/<(/?objec.*?)>/si","",$str); //Filter object tag
$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //Filter noframes tag
$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //Filter noframes tag
$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str) ; //Filter frame tag
$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //Filter frame tag
$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //Filter script tag
$str=preg_replace("/<(/?script.*?)>/si","",$str); //Filter script tags
$str=preg_replace("/javascript/si","Javascript",$str); //Filter script tags
$str=preg_replace("/vbscript/si","Vbscript",$str); //Filter script tags
$str=preg_replace("/on([a-z] )s*=/si","On1=",$str); //Filter script tags
$str=preg_replace("//si","",$str); //Filter script tags |
A simpler way of writing:
?
2 3
|
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;
}
One more one:
?
|