note 21786 rejected from function.nl2br by rasmus
| From: | rasmus@php.net | Date: | Mon, 27 May 2002 06:04:12 +0000 |
| Subject: | note 21786 rejected from function.nl2br by rasmus | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-31064@lists.php.net to get a copy of this message | ||
For those wanting to convert a TAB character (which is ignored in PHP as it's not compatable
with HTML and usually replaced with a bunch of 's for a tab-like effect) into a
line-break plus paragraph indend (as I did, converting written narative text into HTML, requiring
htmlspecialcharacters() to convert my " quote marks into "'s but not wishing it
to convert 's I wrote into the form window into literal ' 's
in the final outcome)
just do as I did, and copy some of the above manual nl2br code helpfully submitted by
trevor@verite.com, and add pseudo-indent thusly:
function replace_nl_brnbsp($string)
{
// cut the value up by \n's
$lines = explode ("
", $string);
$index=0;
while($lines[$index]) {
$lines[$index] = trim($lines[$index]);
if(substr($lines[$index], strlen($lines[$index]) - 4, 4) != "<br>")
$lines[$index] .= "<br> ";
$return_result .= $lines[$index];
$index++;
}
return $return_result;
}
remember, the
$lines = explode ("
", $string);
line requires that carriage-return in the code in order for explode to recognise you're
referring to the <carriage return> character, which presumably isn't writeable any other
way.
you'll notice i just changed
"<br>"
to
"<br> "
--this is assuming you've already done htmlspecialchars() on the text and want a literal
<br>, not a '<br>'
if anyone understood any of that convelusion, I pat them on the back and offer them money. anyway,
just copy the above code and send trevor a cheque and all your clobbered TAB problems will
dissappear =)