Page 1 of 1

how to convert \n to <br>

Posted: Wed Oct 08, 2003 6:41 pm
by Cruzado_Mainfrm
how can i get a text from a db that has \n or \n\r (i really don't know which one but whatever) and convert it to <br>'s so i can display it nicely on a webpage? cuz every line just sticks to the last one and so on

thnx in advance

Posted: Wed Oct 08, 2003 6:48 pm
by McGruff
nl2br()

Posted: Wed Oct 08, 2003 6:50 pm
by Cruzado_Mainfrm
thnx a lot!!!

Posted: Fri Oct 10, 2003 3:46 pm
by Cruzado_Mainfrm
nice, i just noticed that the br's where added like <br /> instead of <br>, is this because i have set the doc to XHTML?

Posted: Fri Oct 10, 2003 3:56 pm
by JAM
No, it's supposed to do that.
string nl2br ( string string)

Returns string with '<br />' inserted before all newlines.

Note: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'.
str_replace() it if you don't want it for some reason. ;)

Posted: Fri Oct 10, 2003 4:02 pm
by Cruzado_Mainfrm
no, i want it, i just wanted to have a clear answer of why this happens :D thnx a lot jam

Posted: Sun Oct 12, 2003 6:54 pm
by McGruff
Any single tag like <img ... /> or <input ... /> needs the "/" as Jam has said: tags in XHTML have to be closed. For example <p> should always be closed with </p> etc.

You don't have to be XHTML compliant but it's probably good practice to start writing tags this way.

Posted: Sun Oct 12, 2003 10:19 pm
by phice
$string = str_replace("\n", "<br>\n", $string);

Posted: Sun Oct 12, 2003 10:40 pm
by murph
there is a function for that, nl2br ( $string );

Posted: Sun Oct 12, 2003 11:01 pm
by volka
and a new cycle starts.... ;-)

Posted: Mon Oct 13, 2003 3:36 am
by twigletmac
which is why you should always read all of the other answers before posting... :lol:

Mac

Posted: Mon Oct 13, 2003 9:50 am
by m3rajk
hmmm... sticky thread "common questions-- read first" with links to some of the more common ones like "any way to make \n to <br> or <br />?"

Posted: Mon Oct 13, 2003 5:49 pm
by JAM
It is in there allready, regarding reading the manual.