Page 1 of 1

\n -> <br>?

Posted: Tue Feb 17, 2004 7:05 pm
by Illusionist
Still working on my forum/guestbook/messageboard thingy. Ig ot the tags, wprking liek i want, so far, but now what i want to do, is sot aht when a user hits the return key and skips lines, to replace those with <br> I've tried replaceing \n, \r, and \n\r, but nothing seems to work.. anyone got any ideas?

Thanks

Posted: Tue Feb 17, 2004 7:11 pm
by Illusionist
hmm, wierd! i was doing '\n' and '<br>' but cahnged it to "\n" and "<br>" and it works now!

Posted: Tue Feb 17, 2004 7:11 pm
by markl999

Posted: Tue Feb 17, 2004 7:12 pm
by tim
check out this:

http://www.php.net/nl2br

:edit - lol, beat to the punch

Posted: Tue Feb 17, 2004 7:48 pm
by Illusionist
lol, thanks.

Which is better to use, str_replace() or nl2br()?

Posted: Tue Feb 17, 2004 7:51 pm
by tim
nl2br() in my eyes.

you can just pull the column name where your "messages" are stored in your sql table n hit it with this:

Code: Select all

<?php
$message = nl2br($row["message"]);
?>

Posted: Tue Feb 17, 2004 8:28 pm
by Rahil
What is /n for?

Posted: Tue Feb 17, 2004 8:32 pm
by Illusionist
lol, its a newline character! lol

Posted: Tue Feb 17, 2004 8:42 pm
by Rahil
Oh is it? lol, why/when do you use it? Why not just use <br>?

Posted: Tue Feb 17, 2004 8:43 pm
by tim
its nothing special...

I use <br> tags still, matter of preference I guess.

Posted: Tue Feb 17, 2004 8:48 pm
by Illusionist
well, one place where i use it is when writing to a file, and i want things on sepereate lines. You can't use <br> there because it is jsut a plain .txt file. So I have to use \n\r to skip a line. And also when writing PHP and your echoing HTML you might add a \n tot he end to keep your HTML clean - when someoen views your source it wont be all on one line, it will be on sepereate lines...

Posted: Tue Feb 17, 2004 10:39 pm
by d3ad1ysp0rk
<br> is HTML, it will be shown in the browser as a newline.
\n is a application newline, meaning anything outside of a browser (notepad, command line, etc)
\r is a windows version of a linebreak.

say you're coding in c++, you're not going to use <br> if you want to space something out in the command prompt; you'd use \n.

edit - a typical use for this is when you're outputting lots of HTML, and don't want it to look messy in notepad:

Code: Select all

<?php
echo "<table width="200" height="200">";
for($i=0;$i<$rows;$i++){
echo "<tr><td>" . $stuff[$i] . "</td></tr>";
}
echo "</table>";
would print out everything inside the for loop on one line
add a "\n" inside the echo statement however, and it makes a line break each time it runs through.

Posted: Tue Feb 17, 2004 11:12 pm
by Illusionist
which is what i said, only in a neater way! hehe