php_nub_qq
php_nub_qq

Reputation: 16065

Replace multiple new lines only

I am trying to do something kind of unusual. I want to replace multiple new lines with two new lines. There are plenty of questions like that but not exactly, see:

Hello\n\n\nWorld\n\n\n\n!

Should become

Hello\n\nWorld\n\n!

This however

Hello\nWorld\n!

Should stay the same. The problem with my regex is that it replaces single new lines with two.

preg_replace('"(\r?\n)+"', "\n\n", $somevar)

Upvotes: 3

Views: 2028

Answers (1)

jambriz
jambriz

Reputation: 1303

something like this?

preg_replace('"(\r?\n){2,}"', "\n\n", $somevar)

Upvotes: 10

Related Questions