<?php                                                                                                                                                                                                                                        
$str = <<<STR
StackOverflow<br>
<br>
<br>
<br>
<br>
is
<br>
<br>
<br>
<br>
a
<br>
community.
STR;
echo preg_replace("!(<br(?: /)?>\s){2,}!s", "<br />\n<br />\n", $str);