I have a string…
$str = 'bob<br /><br />looked so good<br />tonight!';
I want to replace any instance where there is only one <br /> with two.
At the moment I have…
echo preg_replace('/(<br \/>\s*){1}/', '<br /><br />', $str);
But this put’s two <br /> after bob when it should only go after good.
Any help would be great!
Edit: Output should be…
bob<br /><br />looked so good<br /><br />tonight!
Basically you want a
<br />tag that is not preceded nor followed immediately by another. Thus:This assumes that all
<br />tags are literally<br />and not<br>,<br/>, or even<br style="clear:both;">. for better parsing, you should use a DOM parser and look forBRnodes that are not preceded nor followed immediately by anotherBRnode.