PDA

View Full Version : converting to line break


siborg2000
09-03-2002, 07:57 PM
I'm relatively new to php so any help is appreciated...

...I have a simple form that has a text area. The problem I'm having, it that when someone enters data a does a hard return, the form doesn't recognize it and it runs all of the entered text together. If anyone one can resolve this for me, i will be forever in there debt.

Thanks in advance

siborg2000

boywonder
09-03-2002, 08:56 PM
http://www.php.net/manual/en/function.nl2br.php

that help you?
Makes the line breaks show up as <br>'s when you write the text back to a web page.

JohnDubya
10-03-2007, 09:50 PM
FYI, I was having problems with submitting text content to my DB, but this helped me fix it. I put the textarea content into a variable and then ran nl2br on the variable and then put it in my DB. Then, when displaying the data, I did a str_replace() for "\n", "<p></p>, $variable. That worked for me. There may be an easier, more efficient way...if so, someone please clue me in! :)

marek_mar
10-03-2007, 10:31 PM
You should keep the newlines in the data you store in the DB and change them to <br />'s or whatever markup you prefer while displaying the data.

JohnDubya
10-03-2007, 10:35 PM
How exactly is that done? The user enters in the content into the textarea...and before submitting that to the DB, I should mysql_real_escape_string() it...anything else? Then, when pulling the content from the DB, how do I make it recognize the \r and \n that are in the text? I guess I really just don't completely understand how all that works. Any tutorials or anything you guys could show me that would help my understanding? Thanks!

Fumigator
10-04-2007, 12:09 AM
You just said it yourself-- the \n gets stored in the database and you apply nl2br() to the data when you pull it out of the database to display. That way you keep the data flexible for use in situations that don't need html markup (writing it to an xml file, for example).