phptextareapreg-replacecarriage-returnnl2br

Remove more than two carriage returns in textarea form?


I spent hours looking at dozens of different solutions but none of them worked. I am receiving the content of a textarea in a php string and I would like to remove anything which is more than one blank line.

Example ok
Hi how are you, 
// one blank line here so ok to keep
Not too bad thanks

Example not ok
hi how are you
// two lines (or more) here so we remove one and keep the other

not too bad thanks

Does someone know the proper preg_replace to use? Note that I don't want to modify the data (no nl2br() please) as it is easier for me to keep it raw (ios support).


Solution

  • Maybe you should try this:

    preg_replace('/\n\r(\n\r)+/', "\n\r", $str);