Fix a string with an apostrophy

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
rtcary
Forum Newbie
Posts: 6
Joined: Fri Jul 29, 2005 9:24 am

Fix a string with an apostrophy

Post 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;
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Fix a string with an apostrophy

Post by Benjamin »

Please use code tags when posting code.

Code: Select all

 
$str = str_replace("'", '"', $str);
 
rtcary
Forum Newbie
Posts: 6
Joined: Fri Jul 29, 2005 9:24 am

Re: Fix a string with an apostrophy

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