Jump to content


Apostrophe's in strings

  • Please log in to reply
4 replies to this topic

#1 phrak

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 01 April 2006 - 06:38 PM

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?

#2 Zane

  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 01 April 2006 - 06:45 PM

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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank


#3 coldkill

  • Members
  • PipPipPip
  • Advanced Member
  • 159 posts
  • LocationUnited Kingdom

Posted 01 April 2006 - 06:47 PM

str_replace("'", "\'", $text);
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.

You can learn alot from reading the manual

#4 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 April 2006 - 08:25 PM

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.

#5 annihilate

  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 01 April 2006 - 09:12 PM

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.

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);

Personal site: NewEnigma  |  Java and JSF Discussion

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users