How can I trim multiple line breaks?
for instance,
$text ="similique sunt in culpa qui officia
deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore
"
I tried with this answer but it does not work for the case above I think,
$text = preg_replace("/\n+/","\n",trim($text));
The answer I want to get is,
$text ="similique sunt in culpa qui officia
deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore
"
Only single line break is accepted.
Also I want to trim multiple white space at the same time, if I do this below, I can't save any line break!
$text = preg_replace('/\s\s+/', ' ', trim($text));
How can I do both thing in line regex?
Your line breaks in this case are
\r\n
, not\n
:That says "every time 3 or more line breaks are found, replace them with 2 line breaks".
Spaces:
Demo: http://codepad.org/PmDE6cDm
Not sure if this is the best way, but I would use explode. For example:
I hope this helps. Good luck!