A little help with checking input for apostrophes

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
Restrikted
Forum Newbie
Posts: 6
Joined: Sun Jun 01, 2008 10:37 pm

A little help with checking input for apostrophes

Post 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?
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: A little help with checking input for apostrophes

Post 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.
Restrikted
Forum Newbie
Posts: 6
Joined: Sun Jun 01, 2008 10:37 pm

Re: A little help with checking input for apostrophes

Post 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!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: A little help with checking input for apostrophes

Post by Eran »

Restrikted wrote:I actually just found the stripslashes() function on w3's site
So the w3 are giving PHP reference now? interesting
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: A little help with checking input for apostrophes

Post 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.
Post Reply