3

How can I turn this:


...<br />
<br />
...<br />
...<br />
<br />
<br />
...<br />

Into this using PHP:

...<br />
...<br />
...<br />
...

Please notice the deletion of the first and last
even if it's mentioned once. In the middle we keep just one

Isamtron
  • 613
  • 2
  • 8
  • 13
  • 3
    [Friends don't let friends parse HTML with regular expressions.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Ether Sep 22 '10 at 21:46
  • @Ether While I agree in general, this isn't really parsing HTML, just squeezing duplicate strings. – Daniel Vandersluis Sep 22 '10 at 21:48
  • @Daniel: never seen a `
    ` for instance?
    's can have all sorts of attributes just as easily.
    – Wrikken Sep 22 '10 at 22:12
  • @Wrikken as a matter of fact I have (see my answer ;) ). Duplicate really should have been in quotes though. – Daniel Vandersluis Sep 22 '10 at 22:35

2 Answers2

12
preg_replace('#<br />(\s*<br />)+#', '<br />', $your_string);

This will replace instances of more than one <br />, optionally with whitespace separating them, with a single <br />.

Note that <br /> in the pattern matches exactly that. If you wanted to be more defensive, you could change it to <br[^>]*> which would allow for <br> tags that have attributes (ie. <br style="clear: both;" />) or that don't have the trailing slash.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
1

First of all, you should edit your question because the first <br /> doesn't appear.

Then to replace all duplicate <br /> AND the first and the last one, you can do :

$str = <<<EOD
<br />
...<br />
<br />
...<br />
...<br />
<br />
<br />
...<br />
EOD;

$str = preg_replace('~(^<br />\s*)|((?<=<br />)\s*<br />)|(<br />\s*$)~', '', $str);
echo "===\n",$str,"\n===\n";

This will output:

===
...<br />
...<br />
...<br />
...
===
Toto
  • 89,455
  • 62
  • 89
  • 125