Page 1 of 1

Replacing excessive newlines

Posted: Sun Sep 21, 2008 8:09 pm
by Cut
I want to replace 3 or more newlines in a row with 2 newlines. I've tried:

Code: Select all

function preparse($val) {
 $val = preg_replace("/(\r\n\r\n\r\n)+|(\n\n\n|\r\r\r)+/", "\n\n", $val);
 return $val;
}
This does not seem to affect input in any way. Halp?

Re: Replacing excessive newlines

Posted: Sun Sep 21, 2008 8:54 pm
by josh
there can be other hidden characters between newlines (whitespace)

Re: Replacing excessive newlines

Posted: Mon Sep 22, 2008 2:05 am
by prometheuzz
Replaces 3 or more empty lines by two:

Code: Select all

$val = preg_replace('/(\r?\n[ \t]*){3,}/', "\n\n\n", $val);
And yes, "\n\n\n" will result in two empty lines.