简体   繁体   中英

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM