note 59909 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:11:26 +0000 |
| Subject: | note 59909 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195186@lists.php.net to get a copy of this message | ||
Note Submitter: jonasnicklas at hotmail dot com
----
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...