I want to remove multiple <br>
tags which are repeated more than 2times, for example:
This is text blah blah...
<br><br><br><br><br><br>Another text here
Should be:
This is text blah blah...
<br><br>Another text here
I have this:
$str = preg_replace('#(<br\s?/?>)+#', '<br><br>', $str );
But this also replace simple <br>
tags with 2 br
s, how I could change the regular expression for this?
Thanks
Try this one..
#(<br\s?/?>){2,}#
If your <br>
s include spaces in between then this follow this one -
#(<br\s?/?>\s*){2,}#
Try this:
$str = 'This is text blah blah...
<br><br><br><br><br><br>Another text here';
echo preg_replace('#(\s*<br\s*/?>\s*<br\s*/?>\s*)+#', '<br><br>', $str );
Output:
This is text blah blah...
<br><br>Another text here
Update: Thanks for @M42 for his point:
echo preg_replace('#(\s*<br\s*/?>\s*){2,}#', '<br><br>', $str );