Cleaning Up and Storing Text Area Contents

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
lightnb
Forum Newbie
Posts: 13
Joined: Mon Jul 28, 2008 11:13 pm

Cleaning Up and Storing Text Area Contents

Post by lightnb »

I have a medium sized text area that takes user input. Somewhere between one and 5 paragraphs.

What functions should I run on the incoming text to clean it for database storage?

mysql_real_escape_string(), of course, but that doesn't detect and convert non-standard characters, does it?

Specifically:

*When the user pastes content from MS Word, you get 'quotes' that aren't actually quotes and all sorts of other strange characters.

*If the user inserts line breaks, should I convert them to HTML? Should I use HTML_special_chars()? Will that be enough?

*I'm thinking that I do want to allow HTML markup, for formating purposes.

On one of the Zen Cart powered sites I maintain, there's all sorts of strange things in the "product descriptions" field that cause the Google Base feed to choke. Most of this is from 'Word pasting'. I'd like to avoid any non-standard character weirdness this time around.... Any ideas?

And on the database end, do you use BLOB or Text?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Cleaning Up and Storing Text Area Contents

Post by aceconcepts »

In the database i just use TEXT data type.

To make sure the text is stored and restored correctly you could use url_encode()
lightnb
Forum Newbie
Posts: 13
Joined: Mon Jul 28, 2008 11:13 pm

Re: Cleaning Up and Storing Text Area Contents

Post by lightnb »

So I should do something like:

Code: Select all

 
$TextPastedFromWord = $_GET['MyTextArea'];
$CleanText = rawurlencode(htmlentities($TextPastedFromWord));
$TextToInsert = mysql_real_escape_string($CleanText);
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Cleaning Up and Storing Text Area Contents

Post by aceconcepts »

Looks good to me. Give it a whirl.
Post Reply