How do I remove extra <br/"> tags from HTML using PHP?
I am parsing some dirty HTML with PHP that has some redundant tags in it
and I would like to clean them a bit. For instance:
<br> <br /><br /> <br> How would I replace something like this with preg_replace () ?:
<br /><br /> New lines, spaces and differences between <br> , <br/> and <br /> must be considered.
Edit: Basically, I would like to replace each instance of three or more consecutive breaks with only two.
+4
5 answers
Here is what you can use. The first line is found when there are two or more <br> tags (with spaces between them and different types) and replace them with well-formed ones <br /><br /> .
I also included the second line to clear the remaining <br> tags if you want to do this too.
function clean($txt) { $txt=preg_replace("{(<br[\\s]*(>|\/>)\s*){2,}}i", "<br /><br />", $txt); $txt=preg_replace("{(<br[\\s]*(>|\/>)\s*)}i", "<br />", $txt); return $txt; } +6