New line to paragraph in PHP
Here’s an example of how to convert new line (LF – line feed, ‘\n’) or carriage return (CR, ‘\r’) followed by new line (CR+LF, ‘\r\n’) to
or
. In case of double or more breaks we’ll get a paragraph but in case of one break we’ll have
.
$text = 'Lorem ipsum\r\n\r\ndolor sit\r\namet.'; // double or more nl to $text = preg_replace('/(\r?\n){2,}/', ' |
output: Lorem ipsum dolor sit
amet.