coder101
coder101

Reputation: 1605

Replace more than two line break with a single line break

I need to replace more than 2 line breaks in the user input with one single break, also this has to work on all operating systems. the code that i have put together using some examples on SO is as follows:

$post_desc = preg_replace("/[\r\n]+/", "\n", $post_desc);
$post_desc = nl2br($post_desc);

But this is only replacing every line break with only a single one even if it is more than one meaning i need it to replace more than one line breaks into only one. Target is to show paragraphs as paragraphs and replace multiple line breaks between paragraphs in a single one.

Please guide.

Upvotes: 0

Views: 1252

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89649

Instead of replacing duplicate linebreaks and after replace all linebreaks with a <br>, why you don't do it in one step:

$post_desc = preg_replace('/\R+/', '<br>', $post_desc);

Upvotes: 1

vks
vks

Reputation: 67998

([\r\n]){2,}

Try this.Replace by $1.This will only replace linebreaks more than 2.

Upvotes: 1

Related Questions