dextervip dextervip - 2 months ago 8
PHP Question

How to remove duplicate break lines with PHP

I would like to know how could I remove duplicate break lines with PHP considering that the input can be from various OS.

Input Ex.: "02 02 02 02 \r\n\r\n 02 02 02 02 \r\n 02 02 02 02"

Input Ex.: "02 02 02 02 \n\n\n 02 02 02 02 \n\n 02 02 02 02"

Output Ex.: "02 02 02 02 \n 02 02 02 02 \n 02 02 02 02"

Answer

You could use preg_replace:

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

See it working online: ideone