0

I need to know how to display information put in a database longtext field the way it was written.

Like if a user writes in this below:

My life is full of love I fly like
wedding doves I blow passed stop signs
That intersect with hate lines

-

I know what I am
I know who I be
If you can't accept me
Then don't friend me

I want it to display the text from the database just like the user wrote it in the textarea instead of it displaying in one like this:

My life is full of love I fly like wedding doves I blow passed stop signs That intersect with hate lines I know what I am I know who I be If you can't accept me Then don't friend me

How can I could it in PHP to display the information properly using PHP?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
yanike
  • 19
  • 7

4 Answers4

4

Use nl2br(). It converts line breaks (e.g. from textareas) into HTML <br /> tags.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

If you use nl2br on the data, it should appear with approximately the same formatting as it did in the originating textarea.

However, I'd really recommend reading about things like handling SQL injection and escaping output, etc. first as you're quite possibly setting yourself up for some cross site scripting (XSS) issues if you're not careful.

Community
  • 1
  • 1
John Parker
  • 54,048
  • 11
  • 129
  • 129
0

If you want to display a preformated text enclose it in <pre></pre> tags

  <pre>...your text here...</pre>

Many people dislike the <pre> element because it is often abused by bad developers and designers but this is exactly the case it is supposed to be used for.

Most browsers render preformated text different then normal text, so you might have to adjust your stylesheet

body, pre {font-family: ...;font-size:....}
Oliver A.
  • 2,870
  • 2
  • 19
  • 21
0

I can't stress it enough: You should never output user-generated content as it is but be careful that you escape all text you output (see: http://php.net/manual/en/function.htmlspecialchars.php ).

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • I already know about that. I'll be working on the security soon. I'm definitely going to block scripts and other things and force users to use special tags for certain things. – yanike Jan 01 '11 at 12:46