note 43500 deleted from function.nl2br by nlopess
| From: | nlopess@php.net | Date: | Wed, 23 Jun 2004 11:31:24 +0000 |
| Subject: | note 43500 deleted from function.nl2br by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71546@lists.php.net to get a copy of this message | ||
Note Submitter: jsvlrt@yahoo.com
----
I have an admin area, where users can enter text including html. I want the content to be
nl2br'ed, but when they enter something like this:
<b>hello</b>
this content
is on multiple lines
<table>
<tr>
<td>this is a problem</td>
</tr></table>
Each line receives a br, which screws the format of the table up. So, this code:
<?
$nobr = false;
$content = "";
$tmp_content = explode("\n",$row['content']);
foreach($tmp_content as $line)
{
if(strpos($line,"<nobr>")!==false)
{
$nobr = true;
} else if(strpos($line,"</nobr>")!==false)
{
$nobr = false;
} else {
$content .= $line;
if(!$nobr)
{
$content .= "<br />\n";
}
}
}
?>
will nl2br everything that's not enclosed in <nobr>, </nobr> tags. The user can now
enter
<b>hello</b>
this content
is on multiple lines
<nobr>
<table>
<tr>
<td>this is a problem</td>
</tr></table>
</nobr>
and the script will add a <br /> after the first few lines, but not inside the <nobr>
tags.
hope someone finds it helpful.