Jump to content

Apostrophe's in strings


phrak

Recommended Posts

I'm sure this has probably been asked a zillion times but the search wasn't too helpful, so please be gentle with the flames.

I'm a complete php newb and I'm running an eve killboard, that takes a killmail, parses it into strings, and uploads the data to a mysql database. The problem is whenever the killmail contains an apostrophe, it is either read incorrectly and then screws up the database entry or it isn't read at all. I know that you have to put a slash in front of the apostrophe, but is there a way to make the parser check the string for apostrophes and then have it put the slash in, or do I have to tell everyone who uses this php ap to put a slash in front of their apostrophes or ignore them altogether?
Link to comment
Share on other sites

Try
[code]str_replace("'", "\'", $text);[/code]
That will find any " ' "'s in $text and replace them with " \' ".
There is always addslashes which is the default PHP function (from 3 onwards) to add a slash in front of any ' and " characters. But you have to use stripslashes when you want to display the string.
Link to comment
Share on other sites

I include the following code on every page, and for any database queries with form data, apply mysql_real_escape_string to the form variables. You don't need to apply any functions to retrieve from the database, the text in there wil be exactly as you submitted it.

[code]if ( get_magic_quotes_gpc() )
{
   function stripslashes_deep($value)
   {
       $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
       return $value;
   }
$_POST = stripslashes_deep($_POST);
$_GET = stripslashes_deep($_GET);
$_COOKIE = stripslashes_deep($_COOKIE);
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.