How to convert multiple <br/> tag to a single <br/> tag in php

html, php, regex

Solution

You can do this with a regular expression:

preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $input);

This if you pass in your source HTML, this will return a string with a single <br/> replacing every run of them.

Problem

Wanted to convert ``` <br/> <br/> <br/> <br/> <br/> ``` into ``` <br/> ```

Original source