From: Jim Lucas on
Al wrote:
> Anyone have a regex pattern for deleting multiple backslashes e.g., \\\\\\\
>
> I pretty good with regex; but, be damned if I can delete them with
> preg_replace()
>
> I've tried "\\\\" as the manual says
>
> preg_replace("/\\\\/", '', $str);
>
> preg_replace("/(\\\\)+/", '', $str);
>
> preg_replace("/\x5C/", '', $str);
>
> preg_replace("/\\x5c/", '', $str);
>
> And lots of others.
>
> stripslashes() and stripcslashes() are limited.
>
>
>

Might I ask, how are the multiple slashes getting generated in the first place?
Where is the data coming from?

Next question would be: Do you want to completely remove all instances of
multiple backslashes? Or, do you want to replace all instances of multiple
backslashes with a single backslash?

I would try something like this:

<plaintext><?php

$in = '\\\as\\\\asdf\\\asdf\asdf\\\\\asdf\\\\\asdf';

# to remove all backslashes, us this
echo preg_replace('|[\\\\]+|', '', $in).PHP_EOL;

# to remove all backslashes, us this
echo str_replace('\\', '', $in).PHP_EOL;

# to replace consecutive instances of backslashes with a single backslash
echo preg_replace('|[\\\\]+|', '\\', $in).PHP_EOL;

?>
done!


--
Jim Lucas
NOC Manager
541-323-9113
BendTel, Inc.
http://www.bendtel.com
From: Al on


On 3/15/2010 5:03 PM, Jim Lucas wrote:
> Al wrote:
>> Anyone have a regex pattern for deleting multiple backslashes e.g., \\\\\\\
>>
>> I pretty good with regex; but, be damned if I can delete them with
>> preg_replace()
>>
>> I've tried "\\\\" as the manual says
>>
>> preg_replace("/\\\\/", '', $str);
>>
>> preg_replace("/(\\\\)+/", '', $str);
>>
>> preg_replace("/\x5C/", '', $str);
>>
>> preg_replace("/\\x5c/", '', $str);
>>
>> And lots of others.
>>
>> stripslashes() and stripcslashes() are limited.
>>
>>
>>
>
> Might I ask, how are the multiple slashes getting generated in the first place?
> Where is the data coming from?
>
> Next question would be: Do you want to completely remove all instances of
> multiple backslashes? Or, do you want to replace all instances of multiple
> backslashes with a single backslash?
>
> I would try something like this:
>
> <plaintext><?php
>
> $in = '\\\as\\\\asdf\\\asdf\asdf\\\\\asdf\\\\\asdf';
>
> # to remove all backslashes, us this
> echo preg_replace('|[\\\\]+|', '', $in).PHP_EOL;
>
> # to remove all backslashes, us this
> echo str_replace('\\', '', $in).PHP_EOL;
>
> # to replace consecutive instances of backslashes with a single backslash
> echo preg_replace('|[\\\\]+|', '\\', $in).PHP_EOL;
>
> ?>
> done!
>
>

As I reported earlier, problem was my code was reloading a POST array following
the backlash removal. Dumb error on my part.

Re: "Might I ask, how are the multiple slashes getting generated in the first
place? Where is the data coming from?" It comes from a client-side editor where
users can enter them at will. It is my standard practice to do a good job of
protecting users from themselves. I originally just had the usual stripslashes()
but found it didn't take care of users adding several.