Remain formatting in DB and when using ECHO

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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Remain formatting in DB and when using ECHO

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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");
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

works great, thank you.
Post Reply