php Filter HTML Tag Attribute etc Regular Expression Summary

  • 2021-07-18 07:38:06
  • OfStack


$str=preg_replace("/\s+/", " ", $str); // Filter excess carriage return 
$str=preg_replace("/<[ ]+/si","<",$str); // Filter <__("<" Number followed by space )
 
$str=preg_replace("/<\!--.*?-->/si","",$str); // Notes 
$str=preg_replace("/<(\!.*?)>/si","",$str); // Filter DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); // Filter html Label 
$str=preg_replace("/<(\/?head.*?)>/si","",$str); // Filter head Label 
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); // Filter meta Label 
$str=preg_replace("/<(\/?body.*?)>/si","",$str); // Filter body Label 
$str=preg_replace("/<(\/?link.*?)>/si","",$str); // Filter link Label 
$str=preg_replace("/<(\/?form.*?)>/si","",$str); // Filter form Label 
$str=preg_replace("/cookie/si","COOKIE",$str); // Filter COOKIE Label 
 
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); // Filter applet Label 
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); // Filter applet Label 
 
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); // Filter style Label 
$str=preg_replace("/<(\/?style.*?)>/si","",$str); // Filter style Label 
 
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); // Filter title Label 
$str=preg_replace("/<(\/?title.*?)>/si","",$str); // Filter title Label 
 
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); // Filter object Label 
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); // Filter object Label 
 
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); // Filter noframes Label 
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); // Filter noframes Label 
 
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); // Filter frame Label 
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); // Filter frame Label 
 
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); // Filter script Label 
$str=preg_replace("/<(\/?script.*?)>/si","",$str); // Filter script Label 
$str=preg_replace("/javascript/si","Javascript",$str); // Filter script Label 
$str=preg_replace("/vbscript/si","Vbscript",$str); // Filter script Label 
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); // Filter script Label 
$str=preg_replace("/&#/si","& # ",$str); // Filter script Labels, such as javAsCript:alert( 

Clear spaces and wrap lines


function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}

Filtering HTML Attributes

1. Filter regular expressions for all html tags:

 
</?[^>]+>
 
// Filter all html The regular expression for the attribute of the tag:
 
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);

3. Filter the exclusion of regular expressions of html tags (such as exclusion < p > That is, it does not filter < p > ):

</?[^pP/>]+>

4. Filter the enumeration of regular expressions of html tags (for example, filter is needed) < a > < p > < b > Etc.):

</?[aApPbB][^>]*>

5. Exclusion formula of regular expression for filtering the attributes of some html tags (for example, excluding alt attributes, that is, not filtering alt attributes):

\s(?!alt)[a-zA-Z]+=[^\s]*

6. Enumeration of regular expressions that filter the attributes of some html tags (such as alt attributes):

(\s)alt=[^\s]*

PS: Regarding regularity, here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg


Related articles: