Page 1 of 1

A little help with checking input for apostrophes

Posted: Sun Jun 08, 2008 9:41 am
by Restrikted
I've been struggling with this problem for a while now. I'm having a user input some text from a form, but if he puts and apostrophe into the text field ('), when I print the data from the database, it always shows up as \' instead of his regular '. For example if he inputs "my friend's" it'll show up as "my friend\'s". I'm pretty sure this has to do with php recognizing this as a regular apostrophe, and putting the \ in there so I don't get an error (at least I think). So I'm trying to search the string for backslashes and removing them, but I can't seem to get it to work. I keep getting errors. Does anyone have an idea on how to solve this?

Re: A little help with checking input for apostrophes

Posted: Sun Jun 08, 2008 10:00 am
by dbemowsk
PHP has a function called stripslashes. After retrieving your data from the database, pass it through the stripslashes function.
Restrikted wrote:I'm pretty sure this has to do with php recognizing this as a regular apostrophe, and putting the \ in there so I don't get an error
You are correct. The code is probably using either addslashes or mysql_real_escape_string which will escape certain characters for data to be inserted into the database properly. Which then means that you must unescape the data (stripslashes) before presenting it.

Re: A little help with checking input for apostrophes

Posted: Sun Jun 08, 2008 10:56 am
by Restrikted
Haha yeah I'm using mysql_real_escape_string. I actually just found the stripslashes() function on w3's site, and I came here to tell you I didn't need help anymore. Thanks anyway!

Re: A little help with checking input for apostrophes

Posted: Sun Jun 08, 2008 11:37 am
by Eran
Restrikted wrote:I actually just found the stripslashes() function on w3's site
So the w3 are giving PHP reference now? interesting

Re: A little help with checking input for apostrophes

Posted: Sun Jun 08, 2008 11:59 am
by dbemowsk
pytrin, the w3 has been giving PHP references for a while now. I always use the reference at the PHP website though because it is much more detailed.