Page 1 of 1
Remain formatting in DB and when using ECHO
Posted: Sat Oct 07, 2006 9:35 pm
by waradmin
So I have an input text area. and on submit it puts the text into the DB. The formatting stays. But when I echo it, the formatting goes away (for example text like:
this is line one.
this is line 2
turns into:
this is line one this is line 2)
So I decided to use the <pre> tag and echo the database row. It worked, the formatting came back. HOWEVER when a line is more than say 50 characters long, it throws off the table borders (obviously because <pre> does not wrap lines). What is a better way to echo a row but keep the formatting?
Posted: Sat Oct 07, 2006 9:38 pm
by s.dot
You might need to use the function
nl2br() when you want to echo it on a page. Or even store it that way in the DB, if you want.
nl2br() will turn the new lines (\n) into <br /> (line break tag).
Usage:
Code: Select all
echo nl2br("this is line one\n\nthis is line two");
Posted: Sat Oct 07, 2006 9:46 pm
by waradmin
Well when I use text as the data type in the row it keeps the formatting in the DB. I just cant echo it. Now with that function it takes \n and turns it into <br>, but how do you then take an input, and have it add in \n for each new line added when in the DB it doesnt have those characters?
Posted: Sat Oct 07, 2006 9:50 pm
by s.dot
waradmin wrote:Well when I use text as the data type in the row it keeps the formatting in the DB. I just cant echo it. Now with that function it takes \n and turns it into <br>, but how do you then take an input, and have it add in \n for each new line added when in the DB it doesnt have those characters?
It does have those characters. They just aren't visible.
When you type in a textarea
Code: Select all
this is line one
and this is line two
and then store that in the database, it is actually this:
Code: Select all
this is line one\n
\n
and this is line two
(or some similar variant) the \n's are just hidden if you're viewing it (like in PHPMyAdmin)
Posted: Sat Oct 07, 2006 10:12 pm
by waradmin
works great, thank you.