\n -> <br>?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

\n -> <br>?

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

hmm, wierd! i was doing '\n' and '<br>' but cahnged it to "\n" and "<br>" and it works now!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

check out this:

http://www.php.net/nl2br

:edit - lol, beat to the punch
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol, thanks.

Which is better to use, str_replace() or nl2br()?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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"]);
?>
Rahil
Forum Newbie
Posts: 17
Joined: Sun Feb 15, 2004 2:24 pm
Location: Toronto, Ontario, Canada

Post by Rahil »

What is /n for?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol, its a newline character! lol
Rahil
Forum Newbie
Posts: 17
Joined: Sun Feb 15, 2004 2:24 pm
Location: Toronto, Ontario, Canada

Post by Rahil »

Oh is it? lol, why/when do you use it? Why not just use <br>?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

its nothing special...

I use <br> tags still, matter of preference I guess.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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...
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

which is what i said, only in a neater way! hehe
Post Reply