mdhuke
mdhuke

Reputation: 141

Match regex pattern multiple times

I'm trying to write a regex that will match a given pattern between 2 and unlimited times. Effectively I'm just trying to combine multiple line breaks in to one, if any exist at all.

Sample input:

<br><br>
<br>
ABC
<br>
<br>

Expected output:

<br>
ABC
<br>

If the regex was run on the above output then I would expect to see the exact same output.

There could be any amount of whitespace between the <br> tags.

What I've tried:

$html = preg_replace('/(?:<br>\s?){2,}/s', null, $html);

Upvotes: 0

Views: 192

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Just look for a <br> followed by optional space characters 2 or more times and replace with <br>:

$html = preg_replace('/(<br>\s*){2,}/', '<br>', $html);

You might replace with "<br>\n" if you want.

Upvotes: 1

Related Questions