note 91828 modified in function.nl2br by rquadling
| From: | rquadling@php.net | Date: | Mon, 05 Oct 2009 13:20:31 +0000 |
| Subject: | note 91828 modified in function.nl2br by rquadling | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161352@lists.php.net to get a copy of this message | ||
for the <pre> tag problem, the reason why nl2br does that is because it converts all \n to
<br />\n
(see the extra \n there)
an easy hack is to use strtr to replace all \n with just <br />
eg.
<?php
function mynl2br($text) {
return strtr($text, array("\r\n" => '<br />', "\r" =>
'<br />', "\n" => '<br />'));
}
?>
very simple and very fast. the only disadvantage is that anyone reading your nl2br'd html will
see it all on one line.
--was--
for the <pre> tag problem, the reason why nl2br does that is because it converts all \n to
<br />\n
(see the extra \n there)
an easy hack is to use strtr to replace all \n with just <br />
eg.
<?php
function mynl2br($text) {
strtr($text, array("\n" => '<br />', "\r\n"
=>'<br />'));
}
?>
very simple and very fast. the only disadvantage is that anyone reading your nl2br'd html will
see it all on one line.
http://php.net/manual/en/function.nl2br.php