Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cleaning mysql text from line breaks

Tags:

php

How would you go about removing extra line breaks for example:

text text text


text text

And i would like it to be this way

text text
text

i am already using nl2br($desc) in order for the line breaks to appear at all now how to i limit them to 1

like image 688
Vad.Gut Avatar asked Dec 03 '25 19:12

Vad.Gut


1 Answers

Before using nl2br() replace all sequences of carriage return or new line characters with a single newline:

$result = preg_replace('/[\r\n]+/', "\n", $desc);

See it working online: ideone

like image 127
Mark Byers Avatar answered Dec 05 '25 09:12

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!