note 59909 added to function.nl2br
| From: | jonasnicklas at hotmail dot com | Date: | Mon, 19 Dec 2005 22:04:13 +0000 |
| Subject: | note 59909 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100435@lists.php.net to get a copy of this message | ||
Re: emailfire
The function emailfire posted is missing some backslashes, a better alternative to it is:
<?php
$foo = "This\nis a\r\ntest\rmessage";
function nl2brr($text)
{
return preg_replace("/\r\n|\n|\r/", "<br>", $text);
}
echo nl2brr($foo);
# Output: This<br>is a<br>test<br>message
?>
I put both functions in a for-loop and surprisingly preg_replace is a lot faster, I assume this is
because str_replace will search the string several times. If you know what kind of breaks
(Windows/Unix) you are expecting it is obviously better to use str_replace.
A note: Internet Explorer does not have any XHTML support, even browsers that DO have XHTML support
(such as Firefox or Opera) will not parse pages as XHTML unless told so via a Header (for example
through php's header function, like <?php header(Content-type: application/xhtml+xml);
?>), the tag <br /> is invalid in HTML 4. Since all browsers show it correctly anyway
(though according to the specification they shouldn't), you could simply not care...
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59909
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59909&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59909&report=yes
Search -- http://master.php.net/manage/user-notes.php