tags from HTML using PHP? I am parsing some dirty HTML with PHP that has some redundant tags in it and I would like ...">

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
source share
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
source

This should work using a minimal qualifier:

 preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $multibreaks); 

Must match horrific designs <br><br /><br/><br> .

+5
source

this will replace all gaps ... even if they are in uppercase:

 preg_replace('/<br[^>]*>/i', '', $string); 
+3
source

Try:

 preg_replace('/<br\s*\/?>/', '', $inputString); 
0
source

Use str_replace, this is much better for a simple replacement, and you can also pass an array instead of a single search value.

 $newcode = str_replace("<br>", "", $messycode); 
0
source

All Articles