Clearing mysql text from line breaks

How could you remove the extra lines, for example:

text text text


text text

And I would like that to be so.

text text
text

I already use nl2br ($ desc) so line breaks appear in general now, how to limit them 1

+3
source share
2 answers

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

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

See how it works on the Internet: ideone

+5
source

after nl2br()do:

$desc = preg_replace('#(<br[ ]?[\/]?>[\r\n]*)+#','<br/>',$desc);
0
source

All Articles