note 76431 deleted from function.nl2br by nlopess
| From: | nlopess@php.net | Date: | Sun, 15 Jul 2007 11:10:35 +0000 |
| Subject: | note 76431 deleted from function.nl2br by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128538@lists.php.net to get a copy of this message | ||
Note Submitter: lucky760 at yahoo dot com
----
After realizing this would be a problem in allowing users to enter select HTML tags at http://www.VideoSift.com I came up with this simple, yet
somewhat elegant solution to <br /> being inserted into the middle of HTML tags containing
newlines (e.g., "<a \n href=..." becomes "<a <br /> href=...").
There are probably other solutions somewhere on this quite lengthy page, but in case not,
here's my contribution:
<?php
public static function html_nl2br($i_str) {
$i_str = str_replace("\r\n", "\n", $i_str);
$i_str = str_replace("\r", "\n", $i_str);
$lines = split("\n", $i_str);
$i_str = '';
foreach ($lines as $index => $line) {
if (!(strrpos($line, '<') > strrpos($line, '>')))
$lines[$index] .= '<br />';
}
return implode(' ', $lines);
}
?>