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);
posted on 27.07.2019 at 17:50
0pnshow more

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 />
posted on 01.08.2019 at 19:54
0pnshow more
share on facebookshare on twitter
2021 AnswerTabsTermsContact us