note 73479 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:08:27 +0000 |
| Subject: | note 73479 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195167@lists.php.net to get a copy of this message | ||
Note Submitter: moester at g-mail
----
<?php
function nls2p($str)
{
return str_replace('<p></p>', '', '<p>'
. preg_replace('#([\r\n]\s*?[\r\n]){2,}#', '</p>$0<p>',
$str)
. '</p>');
}
?>
Turns two or more consecutive newlines (separated by possible white space) into a
<p>...</p>.
Pass result to regular nl2br() to add <br/> to remaining nl's, eg,
<?php
echo nl2br(nls2p("Paragraph1\n\nParagraph2\n line1\n line2\n"));
?>
result:
<p>Paragraph1</p>
<p>Paragraph2<br/>
line1<br/>
line2<br/></p>