how to convert \n to <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
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

how to convert \n to <br>

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

nl2br()
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

thnx a lot!!!
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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. ;)
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

no, i want it, i just wanted to have a clear answer of why this happens :D thnx a lot jam
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

$string = str_replace("\n", "<br>\n", $string);
Image Image
murph
Forum Commoner
Posts: 29
Joined: Fri Oct 03, 2003 1:28 pm
Location: washington

Post by murph »

there is a function for that, nl2br ( $string );
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and a new cycle starts.... ;-)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

which is why you should always read all of the other answers before posting... :lol:

Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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 />?"
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

It is in there allready, regarding reading the manual.
Post Reply