Kingsley
Kingsley

Reputation: 404

PHP replace more than two <br> to one with preg_replace

My comment has so much 
<br>
<br>
<br>
<br>
<br>
tags

how to replace these br tags to one?

this one doesn't work for me $text = preg_replace('#<br\s*/?>#i', "\n", $text);

Upvotes: 0

Views: 596

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can try this:

$text = preg_replace('~(?:<br\b[^>]*>|\R){2,}~i', "\n", $text);

where \R matches any type of newlines and [^>], all characters except >.

Upvotes: 4

Related Questions