Wanted to convert
<br/>
<br/>
<br/>
<br/>
<br/>
into
<br/>
You can do this with a regular expression:
preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $input);
This if you pass in your source HTML, this will return a string with a single <br/> replacing every run of them.
Mine is almost exactly the same as levik's (+1), just accounting for some different br formatting:
preg_replace('/(<br[^>]*>\s*){2,}/', '<br/>', $sInput);
Enhanced readability, shorter, produces correct output regardless of attributes:
preg_replace('{(<br[^>]*>\s*)+}', '<br/>', $input);
Thanks all.. Used Jakemcgraw's (+1) version
Just added the case insensative option..
{(<br[^>]*>\s*)+}i
Great tool to test those Regular expressions is:
http://www.spaweditor.com/scripts/regex/index.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With