note 56737 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:11:34 +0000 |
| Subject: | note 56737 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195187@lists.php.net to get a copy of this message | ||
Note Submitter: thunberg at gmail dot com
----
I wrote this because I wanted users to be able to do basic layout formatting by hitting enter in a
textbox but still wanted to allow HTML elements (tables, lists, etc). The problem was in order for
the output to be correct with nl2br the HTML had to be "scrunched" up so newlines
wouldn't be converted; the following function solved my problem so I figured I'd share.
<?php
function nl2br_skip_html($string)
{
// remove any carriage returns (mysql)
$string = str_replace("\r", '', $string);
// replace any newlines that aren't preceded by a > with a <br />
$string = preg_replace('/(?<!>)\n/', "<br />\n", $string);
return $string;
}
?>