tags:

views:

35

answers:

3

Having a mental block....

I know this is so easy.... but nothing is going my way today!

Can anyone help me with a quick function to search a string and escape all apostrophes in php. Thanks.

+3  A: 

Use str_replace:

$str = ...
$str = str_replace("'", "\\'", $str);
Jacob Relkin
+2  A: 

You can use:

$changed_string = str_replace("'","\\'",$input_string)

You can also make use of the library function addslashes. Remember it will add slashes in front of single quote ('), double quote ("), backslash () and NULL.

codaddict
+2  A: 

addslashes

thomasrutter