black_belt
black_belt

Reputation: 6799

how to display the stored data in just the way it was formatted by user?

I have a textarea as an input in my HTML form. When any user writes something like following (I am trying to point out the line breaks):

The quick brown fox jumps **over**
the lazy dog

And when it is stored into database and I retrieved it and echo it appears without the line breaks, it looks like following:

The quick brown fox jumps over the lazy dog

Could you please tell me how to display the stored data in just the way it was formatted by the user?

When I display data I just retrieve information from database just the usual way and then just echo it like this-- <? echo $value_text ;?>

Thanks in Advance :0

Upvotes: 0

Views: 65

Answers (3)

DaveRandom
DaveRandom

Reputation: 88647

You should do

echo nl2br(htmlspecialchars($value_text));

nl2br() - htmlspecialchars()

Upvotes: 3

Brian Glaz
Brian Glaz

Reputation: 15676

You just need to wrap the text in an nl2br() function like this: <? echo nl2br($value_text);?>. This will convert all new lines into <br> tags.

When your page loads, the php echo gets interpreted as html. As you know, new lines in HTML don't affect the layout of a page. This is why you need every line break converted into a <br> tag.

Upvotes: 1

David Houde
David Houde

Reputation: 4778

Are you sure the line breaks don't exist?

If you are using this on a website, you may need to replace the line break with a
tag.

echo nl2br($data);

http://us.php.net/nl2br

Or you can alternatively wrap the text in <pre> tag's

Upvotes: 1

Related Questions