views:

87

answers:

2

How to add a backslash before single quotes using preg_replace() php function ?

+4  A: 

In case you are not trying to escape Database input:

$string = str_replace("'", "\\'", $string);

In case you are:

Please consider using the appropriate escaping function. (E.g. mysql_real_escape_string) or just use prepared statements.

edorian
+1 for "Please consider using the appropriate escaping functions".
NawaMan
A: 

just use the function str_replace to replace every instance of ' to \'

dejavu