User Contributed Notes: nl2br
davidbullock@tech-center.com
22-Nov-1999 11:06
If you use nl2br in conjunction with htmlspecialchars, you should process the string with nl2br after htmlspecialchars to prevent htmlspecialchars from escaping your newly generated symbols.
chrisr@broadcastmusic.com
13-Jan-2000 01:59
Why no br2nl?
Granted it's easy to write a br2nl, but the same could be said of nl2br.
howardh@telus.net
04-Mar-2000 02:28
the above doesn't seem make sense. a newline char should be "\n", therefore the str_replace should be as follows:
<pre>
str_replace(" ","\n",$s);
</pre>
This will return the string back to a reasonable semblance of its former self (prior to nl2br).
stefan@desire.ch
25-Apr-2000 05:54
i had the "black box" problem. i now use
$text = str_replace("\r\n"," ",$text);
which seems to work (what the heck is \r !?)
kris@mha.ca
25-Apr-2000 08:22
The black box (its because windows doens't have a font character to display the \r) and \r\n are windows'isms. To windows a linefeed(\n) doesn't mean anything by itself, and it needs a carriage return (\r) for the newline to be valid. That is why a CrLf (carriage return linefeed) is required (\r\n) in windows, and not in unix. The black box you see is because php's nl2br is stripping the \n but not the \r. I guess the only solution in windows is not use nl2br and use <PRE>$value = str_replace("\\r\\n"," ",$value);
</PRE>
fsace@hotmail.com
08-May-2000 02:12
i have found if i use str_replace("\n", " ", $s);
works but sometimes leaves the newline.
so i split it into an array:
$arr = explode("\n", $s);
$n = 0;
while ($n < count($arr)) {
$arr[$n] = Chop($arr[$n]) . " ";
$n++;
}
$ss = implode("", $arr);
max@tocrawl.com
08-Jul-2000 11:38
I need to get submitted text into MySQL DB and then on requerst pull it out in HTML format, submission field has breaks, I mean if in text area you hit return it makes a line for you. nl2br will replace all those lines with
<PRE>
</PRE>
but, as every webmaster knows tag is much better then a couple s .
Any Idea how to change/replace 2 BRs with one P tag?
Thanks
max@tocrawl.com
08-Jul-2000 11:42
Sorry, above I was using <> which didn't show up....
what I was saing was:
I need to get submitted text into MySQL DB and then on requerst pull it out in HTML format, submission field has breaks, I mean if in text area you hit return it makes a line for you. nl2br will replace all those lines with
<PRE>
<BR>
<BR>
</PRE>
but, as every webmaster knows <P> tag is much better then a couple <BR>s .
Any Idea how to change/replace 2 BRs with one P tag?
Thanks
malcolmf@nkaos.com
13-Jul-2000 02:21
As I sit here trying to conform code to a clients XML-compatible standard I get to thinking that maybe it should be replacing \n with <BR /> instead of , just a thought....
jen_woodhead@hotmail.com
28-Jul-2000 05:04
I ithnk the problem that you are hitting concerns, windows using /r/n, unix using /n and the mac using /r all to represent the end of the line. This can inpart be remedied by using DOS2UNIX (not php) or by using reg exps to remove both symbols.