| Author |
Using Regex with backslashes
|
|
| RedGalaxy UK 2005-11-26, 6:56 pm |
| Hi,
I'm trying to write a sub to do the same thing PHP's addslashes()
statement does. I have data which needs inserting into a MySQL database
and need to escape certain characters.
' needs to be \'
" needs to be \"
\ needs to be \\
I'm not 100% sure if there are any others, but I can get my script to
do the first two, just not the \ to \\.
Does anyone know how I could get my script to do this? I've tried all
sorts of things and looked on the Internet and couldn't find anything.
Any help would be greatly appreciated.
Thanks
Paul
| |
| usenet@DavidFilmer.com 2005-11-26, 6:56 pm |
| RedGalaxy UK wrote:
<snip>
> \ needs to be \\
>
> I'm not 100% sure if there are any others, but I can get my script to
> do the first two, just not the \ to \\.
You need to escape the backslash. You do that with a backslash. So \\
is one escaped backslash, and \\\\ is two escaped backslashes. ie:
$foo =~ s!\\!\\\\!g;
| |
| RedGalaxy UK 2005-11-26, 9:55 pm |
| Just tried it out; it seems to be ignoring the fact that there are
backslashes in the original string. The only other thing I can think of
is just through the original database and changing any occurrence of \
to either \\ or /. Shouldn't have to really. PHP can do it! lol. Thanks
though.
| |
| vladlr@gmail.com 2005-11-28, 6:57 pm |
| Try to convert all the slashes first.
s|\\|\\\\|g;
s|\'|\\\'|g;
s|\"|\\\"|g;
|
|
|
|