note 21786 added to function.nl2br

From: Date: Mon, 27 May 2002 04:57:12 +0000
Subject: note 21786 added to function.nl2br
Groups: php.notes 
Request: Send a blank email to php-notes+get-31063@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 &nbsp;'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 &quot;'s but not wishing it to convert &nbsp;'s I wrote into the form window into literal '&amp;nbsp;'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) != "&lt;br&gt;") $lines[$index] .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $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 "&lt;br&gt;" to "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" --this is assuming you've already done htmlspecialchars() on the text and want a literal <br>, not a '&lt;br&gt;' 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 =) -- http://www.php.net/manual/en/function.nl2br.php http://master.php.net/manage/user-notes.php?action=edit+21786 http://master.php.net/manage/user-notes.php?action=delete+21786 http://master.php.net/manage/user-notes.php?action=reject+21786

« previous php.notes (#31063) next »