escaping (backslashing) backslashes?
| From: | Jack McKinney | Date: | Wed, 04 Oct 2000 16:52:31 +0000 |
| Subject: | escaping (backslashing) backslashes? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-3316@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Before putting a string into a query for MySQL, one should escape
the quotes in it:
$value = ereg_replace("'","''",$value);
or:
$value = ereg_replace("'","\\'",$value);
This second one actually works in that MySQL accepts the backslash as
an escape, and two backslashes in PHP get escaped into one, so that the
string only has the single backslash in front of the quote, but not two.
Suppose, however, that you need to insert a backslash into the database.
Specifically, the user enters a backslash into a form. I'd like to escape
this. In MySQL, doubling the backslash (i.e., backslashing the backslash)
does the trick:
UPDATE sometable SET somefield = '\\'.
However, this PHP statement:
$value = ereg_replace("\\","\\\\",$value)
gives me:
Warning: REG_EESCAPE in /var/www/docs/test.php on line 5
This only occurs at runtime (i.e., the page parses and displays, but
when I submit the form, thus causing the ereg_replace line to actually
be executed, I get this error).
How do I escape the escape?
- --
"Restore your inalienable human rights. Jack McKinney
Vote Libertarian. http://www.lp.org http://www.lorentz.com
F4 A0 65 67 58 77 AF 9B FC B3 C5 6B 55 36 94 A6 jackmc@lorentz.com
-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
iQCVAwUBOdtgTUZx0BGJTwrZAQESrwP+OGaas699RtSglFdmXLIQFHfh8J5LDCmd
BOUS/07+87q/cNVGhm0K7y96AzdOUDyKKK8YLMNmad5SGXo+ocbAVEN0xpkHJPIJ
YrVw9au0kltVEieBgwxgO7NNs4syOV3CWvD1QZNwusZhY2nPHm9uFgvuA7+4GNfx
4iKLCyVlP/A=
=DeL1
-----END PGP SIGNATURE-----