Wanted to convert
<br/>
<br/>
<br/>
<br/>
<br/>
into
<br/>
Wanted to convert
<br/>
<br/>
<br/>
<br/>
<br/>
into
<br/>
Use a regular expression to match <br/>
one or more times, then use preg_replace (or similar) to replace with <br/>
such as levik's reply.
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.
You probably want to use a Regular Expression. I haven't tested the following, but I believe it's right.
$text = preg_replace( "/(<br\s?\/?>)+/i","<br />", $text );
Mine is almost exactly the same as levik's (+1), just accounting for some different br formatting:
preg_replace('/(<br[^>]*>\s*){2,}/', '<br/>', $sInput);
without preg_replace, but works only in PHP 5.0.0+
$a = '<br /><br /><br /><br /><br />';
while(($a = str_ireplace('<br /><br />', '<br />', $a, $count)) && $count > 0)
{}
// $a becomes '<br />'
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:
A fast, non regular-expression approach:
while(strstr($input, "<br/><br/>"))
{
$input = str_replace("<br/><br/>", "<br/>", $input);
}