1

Topic: Help - Regular Expressions/String Replacement

The aim is to locate two <br />'s in a row which are followed by anthing which is not a <br /> and then replace the two line breaks with something else. The two line breaks could appear at the end of a long string of line breaks and I only want to replace the last two. I also don't want to touch whatever comes after the last two line breaks. Is there a simple one line server/speed friendly bit of code which accomplishes this. If you want a clearer example

blah blah
<br />
<br />
<br />
<br />
blah blah

becomes

blah blah
<br />
<br />
</p>
<p>
blah blah

Re: Help - Regular Expressions/String Replacement

Something like this should do the job:

$text = preg_replace('#<br />\s*?<br />(?!\s*<br />)#i', "</p>\n<p>", $text);
"Programming is like sex: one mistake and you have to support it for the rest of your life."

3

Re: Help - Regular Expressions/String Replacement

Great, that works. With the addition of a few tabs the output is a joy to behold.