How to remove / replace multiple <br /> line breaks in PHP ?

I need to replace multiple <br /> line break tags with one single <br /> tag. I have tried to do so by Regex pattern below, but it doesn't work. Can you please give me suggestion how should the Regex pattern look like, to make it work correctly ?

preg_replace("/(<br\s*\/>)+/", "<br />", $content);
0
give a positive ratinggive a negative rating
27 Jul 2019 at 05:50 PM
Hi,

To replace multiple <br /> line breaks with single line break, you have to use the following Regex pattern:

$updatedcontent = preg_replace("/(<br\s*\/>\s*)+/", "<br />", $content);

When you are using nl2br() function to convert the content from textarea, there is often some whitespace after each line break tag, so the code looks like:

<br /> <br /> <br />
0
give a positive ratinggive a negative rating
01 Aug 2019 at 07:54 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us