Deleting multiple backslashes; regex?
Deleting multiple backslashes; regex?
am 15.03.2010 20:03:12 von Al
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Deleting multiple backslashes; regex?
am 15.03.2010 20:11:09 von Ashley Sheridan
--=-RfF0RvRCMG7T8WKhnavW
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Mon, 2010-03-15 at 15:03 -0400, 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.
>
>
>
What about this:
$str = 'text\\dfnfg\\dhdsg\\\\';
echo preg_replace('/\\\{2,}/', '', $str);
I think what was catching you out was that you were escaping the \ once
for the regex, but then it needed it again because it was in a single
quoted string, meaning 3 slashes in total. Took me a little while to
figure that out as well!
The {2,} part just tells it to only remove the slashes where they occur
in runs of two or more.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-RfF0RvRCMG7T8WKhnavW--
Re: Deleting multiple backslashes; regex?
am 15.03.2010 20:30:58 von Ashley Sheridan
--=-zwbZpg/lVgo39fpwwy5t
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Mon, 2010-03-15 at 15:35 -0400, Al Rider wrote:
>
>
> On 3/15/2010 3:11 PM, Ashley Sheridan wrote:
>
> > On Mon, 2010-03-15 at 15:03 -0400, 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.
> > >
> > >
> > >
> > >
> >
> >
> > What about this:
> >
> > $str = 'text\\dfnfg\\dhdsg\\\\';
> > echo preg_replace('/\\\{2,}/', '', $str);
> >
> > I think what was catching you out was that you were escaping the \
> > once for the regex, but then it needed it again because it was in a
> > single quoted string, meaning 3 slashes in total. Took me a little
> > while to figure that out as well!
> >
> > The {2,} part just tells it to only remove the slashes where they
> > occur in runs of two or more.
> >
>
>
> I agree, it should work; but, it doesn't.
>
> Most everything that looks reasonable works in The Regex Coach,
> including you suggestion.
>
> I'm starting to think there is a bug in the regex engine; haven't
> looked yet. It's doubtful though because the bug would screw existing
> code.
>
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
I tried the code I just gave you and it's working, so I'm not sure now
what issue you're having with it?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-zwbZpg/lVgo39fpwwy5t--
Re: Deleting multiple backslashes; regex?
am 15.03.2010 21:09:02 von james stojan
--0016367fa3dec920ea0481dc73b7
Content-Type: text/plain; charset=ISO-8859-1
Double check your code I came up with the same solution as Ash did and it
worked fine in Wamp.
On Mon, Mar 15, 2010 at 3:30 PM, Ashley Sheridan
wrote:
> On Mon, 2010-03-15 at 15:35 -0400, Al Rider wrote:
>
> >
> >
> > On 3/15/2010 3:11 PM, Ashley Sheridan wrote:
> >
> > > On Mon, 2010-03-15 at 15:03 -0400, 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.
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > > What about this:
> > >
> > > $str = 'text\\dfnfg\\dhdsg\\\\';
> > > echo preg_replace('/\\\{2,}/', '', $str);
> > >
> > > I think what was catching you out was that you were escaping the \
> > > once for the regex, but then it needed it again because it was in a
> > > single quoted string, meaning 3 slashes in total. Took me a little
> > > while to figure that out as well!
> > >
> > > The {2,} part just tells it to only remove the slashes where they
> > > occur in runs of two or more.
> > >
> >
> >
> > I agree, it should work; but, it doesn't.
> >
> > Most everything that looks reasonable works in The Regex Coach,
> > including you suggestion.
> >
> > I'm starting to think there is a bug in the regex engine; haven't
> > looked yet. It's doubtful though because the bug would screw existing
> > code.
> >
> > > Thanks,
> > > Ash
> > > http://www.ashleysheridan.co.uk
> > >
> > >
> > >
>
>
> I tried the code I just gave you and it's working, so I'm not sure now
> what issue you're having with it?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
--0016367fa3dec920ea0481dc73b7--
Re: Deleting multiple backslashes; regex?
am 15.03.2010 21:17:14 von Al
On 3/15/2010 3:30 PM, Ashley Sheridan wrote:
> On Mon, 2010-03-15 at 15:35 -0400, Al Rider wrote:
>
>>
>>
>> On 3/15/2010 3:11 PM, Ashley Sheridan wrote:
>>
>>> On Mon, 2010-03-15 at 15:03 -0400, 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.
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> What about this:
>>>
>>> $str = 'text\\dfnfg\\dhdsg\\\\';
>>> echo preg_replace('/\\\{2,}/', '', $str);
>>>
>>> I think what was catching you out was that you were escaping the \
>>> once for the regex, but then it needed it again because it was in a
>>> single quoted string, meaning 3 slashes in total. Took me a little
>>> while to figure that out as well!
>>>
>>> The {2,} part just tells it to only remove the slashes where they
>>> occur in runs of two or more.
>>>
>>
>>
>> I agree, it should work; but, it doesn't.
>>
>> Most everything that looks reasonable works in The Regex Coach,
>> including you suggestion.
>>
>> I'm starting to think there is a bug in the regex engine; haven't
>> looked yet. It's doubtful though because the bug would screw existing
>> code.
>>
>>> Thanks,
>>> Ash
>>> http://www.ashleysheridan.co.uk
>>>
>>>
>>>
>
>
> I tried the code I just gave you and it's working, so I'm not sure now
> what issue you're having with it?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
Thanks ever so much guys. Sometimes it's helpful when someone else confirms
something must work.
Problem was that I had a series of cleanup and security checks on a POST array
and inadvertently used the original POST array in one of the steps following the
backslash remove step, so obviously it didn't work.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Deleting multiple backslashes; regex?
am 15.03.2010 22:03:16 von List Manager
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:
$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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Deleting multiple backslashes; regex?
am 16.03.2010 16:23:46 von Al
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:
>
>
>
> $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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php