Jump to content

Archived

This topic is now archived and is closed to further replies.

phrak

Apostrophe's in strings

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?

Share this post


Link to post
Share on other sites
go to [a href=\"http://php.net/addslashes\" target=\"_blank\"]http://php.net/addslashes[/a]
and learn the function

it should fix it all up

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
actually you dont have to use stripslashes if you use addslashes as PHP will strip them out automatically as it'll see you are eascaping a single/double quote or characters.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.