Page 1 of 1

Fix a string with an apostrophy

Posted: Wed Jun 11, 2008 4:23 pm
by rtcary
For SQL Server, I need to replace an apostrophe with a double apostrophe. Is there a built in function that will do it?

Thank you...

Todd

/* Fix a string with a apostrophy */
function fix_string($string) {
for ($i=0;$i < strlen($string);$i++) {
if ($string[$i] == "'") $result = $result . "'";
$result = $result . $string[$i];
}
return $result;
}

Re: Fix a string with an apostrophy

Posted: Wed Jun 11, 2008 5:00 pm
by Benjamin
Please use code tags when posting code.

Code: Select all

 
$str = str_replace("'", '"', $str);
 

Re: Fix a string with an apostrophy

Posted: Wed Jun 11, 2008 11:55 pm
by rtcary
Thanks...simple...slick.

I need to replace a single apostrophe with two apostrophes, but that is a simple change to what you gave (which is hard to see in the example below.

Code: Select all

 
$str = str_replace("'", "''", $str);
 
Todd