Author Topic: Displaying stored text correctly  (Read 1059 times)

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Displaying stored text correctly
« on: July 01, 2007, 10:29:50 AM »
Ok, I've come across this problem multiple times and I need some help here.

I have a form with a textarea. The user types some text into the textarea and this text will then be stored in the db.

Now:

How the hell do I display it correctly? Meaning how do I add line breaks (nl2br doesn't seem to work), etc.

Right now I'm doing

1st: strip_tags
2nd: nl2br

But still, I get one big line of text instead of a nicely spereated text.

(I need it for sending messages and my forum)

Offline beam

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
  • Dance Commander
    • View Profile
Re: Displaying stored text correctly
« Reply #1 on: July 01, 2007, 12:54:38 PM »
Well, this is the code I use for my forum:

Preparation, before inserting it into the database:
Code: [Select]
$message = nl2br(addslashes(htmlentities($_POST['body'])));

Displaying it from the database:
Code: [Select]
$message = stripslashes($thread['body']);

The problem might be the way you're doing it.

If you're doing it strip_tags(nl2br($text)), then strip_tags will remove the br's inserted by nl2br(). It should then be nl2br(strip_tags($text)).

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Displaying stored text correctly
« Reply #2 on: July 01, 2007, 01:28:35 PM »
woo!! thanks a lot :)

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Displaying stored text correctly
« Reply #3 on: July 04, 2007, 01:52:40 PM »
don't forget stripslashes in that code above our you'll get messages like:

You\'ll always get slash single/double quote

Creating online addictions, one game at a time:

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Displaying stored text correctly
« Reply #4 on: July 09, 2007, 07:54:14 AM »
Well, this is the code I use for my forum:

Preparation, before inserting it into the database:
Code: [Select]
$message = nl2br(addslashes(htmlentities($_POST['body'])));

Displaying it from the database:
Code: [Select]
$message = stripslashes($thread['body']);

The problem might be the way you're doing it.

If you're doing it strip_tags(nl2br($text)), then strip_tags will remove the br's inserted by nl2br(). It should then be nl2br(strip_tags($text)).

For something like a forum message where the user can edit the text over and over again, it's better to use nl2br() only when the text is being viewed, not when it is being stored in a table. Otherwise, each time the user edits the text, they will get lots of <br> tags in their message.




Edit:
oh, and I'm making a mail system right now, and I'm trying to make sure all the messages are sanitized properly.
Here's what I've got:

$insert['body'] = htmlentities$_POST['body'], ENT_QUOTES);
$insert['body'] = (!get_magic_quotes_gpc())?addslashes($insert['body']):$insert['body'];


Would that work fine? Anybody notice any limitations or ways to get around it?
Thanks :D
« Last Edit: July 09, 2007, 08:20:41 AM by Zeggy »

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Displaying stored text correctly
« Reply #5 on: July 11, 2007, 04:45:21 AM »
Zeg I thought you used ADODB? If so for storing info in the db just use the ? syntax and you don't need to worry about scrubbing the data before inserting it into the db.

To display it though what you have looks good :)
Creating online addictions, one game at a time:

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal