SoFunction
Updated on 2025-03-01

Regular expressions for filtering commonly used tags in PHP

We often use PHP to filter some tags, such as filtering link tags, filtering script tags, etc. The following is a regular expression code for PHP to filter commonly used tags:

$str=preg_replace("/\s+/", " ", $str); //Filter excess return$str=preg_replace("/&lt;[ ]+/si","&lt;",$str); //Filter <__("<" sign with spaces after it)$str=preg_replace("/&lt;\!–.*?–&gt;/si","",$str); //Note$str=preg_replace("/&lt;(\!.*?)&gt;/si","",$str); //Filter DOCTYPE$str=preg_replace("/&lt;(\/?html.*?)&gt;/si","",$str); //Filter html tags$str=preg_replace("/&lt;(\/?head.*?)&gt;/si","",$str); //Filter head tag$str=preg_replace("/&lt;(\/?meta.*?)&gt;/si","",$str); //Filter meta tags$str=preg_replace("/&lt;(\/?body.*?)&gt;/si","",$str); //Filter body tag$str=preg_replace("/&lt;(\/?link.*?)&gt;/si","",$str); //Filter link tags$str=preg_replace("/&lt;(\/?form.*?)&gt;/si","",$str); //Filter form tags$str=preg_replace("/cookie/si","COOKIE",$str); //Filter COOKIE tags$str=preg_replace("/&lt;(applet.*?)&gt;(.*?)&lt;(\/applet.*?)&gt;/si","",$str); //Filter applet tags$str=preg_replace("/&lt;(\/?applet.*?)&gt;/si","",$str); //Filter applet tags$str=preg_replace("/&lt;(style.*?)&gt;(.*?)&lt;(\/style.*?)&gt;/si","",$str); //Filter style tags$str=preg_replace("/&lt;(\/?style.*?)&gt;/si","",$str); //Filter style tags$str=preg_replace("/&lt;(title.*?)&gt;(.*?)&lt;(\/title.*?)&gt;/si","",$str); //Filter title tag$str=preg_replace("/&lt;(\/?title.*?)&gt;/si","",$str); //Filter title tag$str=preg_replace("/&lt;(object.*?)&gt;(.*?)&lt;(\/object.*?)&gt;/si","",$str); //Filter object tags$str=preg_replace("/&lt;(\/?objec.*?)&gt;/si","",$str); //Filter object tags$str=preg_replace("/&lt;(noframes.*?)&gt;(.*?)&lt;(\/noframes.*?)&gt;/si","",$str); //Filter noframes tags$str=preg_replace("/&lt;(\/?noframes.*?)&gt;/si","",$str); //Filter noframes tags$str=preg_replace("/&lt;(i?frame.*?)&gt;(.*?)&lt;(\/i?frame.*?)&gt;/si","",$str); //Filter frame tags$str=preg_replace("/&lt;(\/?i?frame.*?)&gt;/si","",$str); //Filter frame tags$str=preg_replace("/&lt;(script.*?)&gt;(.*?)&lt;(\/script.*?)&gt;/si","",$str); //Filter script tags$str=preg_replace("/&lt;(\/?script.*?)&gt;/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","On\\1=",$str); //Filter script tags$str=preg_replace("/&amp;#/si","&amp;#",$str); //filterscriptLabel