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"
You could use preg_replace:
$s = preg_replace("/[\r\n]+/", "\n", $s);
See it working online: ideone
More quickly is to replace only 2+ line breaks :) :
$s = preg_replace("/([\r\n]{4,}|[\n]{2,}|[\r]{2,})/", "\n", $s);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With