preg_replace with \'s and "'s
| From: | Antonio | Date: | Sat, 12 Aug 2000 20:48:12 +0000 |
| Subject: | preg_replace with \'s and "'s | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11478@lists.php.net to get a copy of this message | ||
Hi-
I need to search a string for single and double quotation marks, and
replace them with escaped versions (\", \'), because I sending the string
as a value to a MySql database (and it doesn't accept non-escaped characters).
First of all, I was wondering if there is a way to escape all characters from
a string that have a special meaning in SQL (without having to enter all
characters into an array and do a search and replace with them).
This is how I've been doing it:
$value="String with \"Double Quotations\" and 'Single Quotations'";
$value=preg_replace("/\"/", "/\\\"/", $value);
$value=preg_replace("/'/", "/\\'/", $value);
print $value;
However, php returns an error on the preg_replace commands. Apparently I
can't search and replace with/for \'s, "'s, 's?
I've been playing around with this for quite some time, and seem to have
come up against a brick wall. I don't know how to fix it. Any tips,
suggestions, fixes, etc would be much appreciated!
Antonio