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:

<?php<br /> <br /> $in = '\\\as\\\\asdf\\\asdf\asdf\\\\\asdf\\\\\asdf';<br /> <br /> # to remove all backslashes, us this<br /> echo preg_replace('|[\\\\]+|', '', $in).PHP_EOL;<br /> <br /> # to remove all backslashes, us this<br /> echo str_replace('\\', '', $in).PHP_EOL;<br /> <br /> # to replace consecutive instances of backslashes with a single backslash<br /> echo preg_replace('|[\\\\]+|', '\\', $in).PHP_EOL;<br /> <br /> ?><br /> done!<br /> <br /> <br /> -- <br /> Jim Lucas<br /> NOC Manager<br /> 541-323-9113<br /> BendTel, Inc.<br /> http://www.bendtel.com<br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <article> <h2>Re: Deleting multiple backslashes; regex?</h2><span>am 16.03.2010 16:23:46 von Al</span> <p>On 3/15/2010 5:03 PM, Jim Lucas wrote:<br /> > Al wrote:<br /> >> Anyone have a regex pattern for deleting multiple backslashes e.g., \\\\\\\<br /> >><br /> >> I pretty good with regex; but, be damned if I can delete them with<br /> >> preg_replace()<br /> >><br /> >> I've tried "\\\\" as the manual says<br /> >><br /> >> preg_replace("/\\\\/", '', $str);<br /> >><br /> >> preg_replace("/(\\\\)+/", '', $str);<br /> >><br /> >> preg_replace("/\x5C/", '', $str);<br /> >><br /> >> preg_replace("/\\x5c/", '', $str);<br /> >><br /> >> And lots of others.<br /> >><br /> >> stripslashes() and stripcslashes() are limited.<br /> >><br /> >><br /> >><br /> ><br /> > Might I ask, how are the multiple slashes getting generated in the first place?<br /> > Where is the data coming from?<br /> ><br /> > Next question would be: Do you want to completely remove all instances of<br /> > multiple backslashes? Or, do you want to replace all instances of multiple<br /> > backslashes with a single backslash?<br /> ><br /> > I would try something like this:<br /> ><br /> > <plaintext><?php<br /> ><br /> > $in = '\\\as\\\\asdf\\\asdf\asdf\\\\\asdf\\\\\asdf';<br /> ><br /> > # to remove all backslashes, us this<br /> > echo preg_replace('|[\\\\]+|', '', $in).PHP_EOL;<br /> ><br /> > # to remove all backslashes, us this<br /> > echo str_replace('\\', '', $in).PHP_EOL;<br /> ><br /> > # to replace consecutive instances of backslashes with a single backslash<br /> > echo preg_replace('|[\\\\]+|', '\\', $in).PHP_EOL;<br /> ><br /> > ?><br /> > done!<br /> ><br /> ><br /> <br /> As I reported earlier, problem was my code was reloading a POST array following <br /> the backlash removal. Dumb error on my part.<br /> <br /> Re: "Might I ask, how are the multiple slashes getting generated in the first <br /> place? Where is the data coming from?" It comes from a client-side editor where <br /> users can enter them at will. It is my standard practice to do a good job of <br /> protecting users from themselves. I originally just had the usual stripslashes() <br /> but found it didn't take care of users adding several.<br /> <br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <footer> <a href="/">Index</a> | <a href="/impressum.php">Impressum</a> | <a href="/datenschutz.php">Datenschutz</a> | <a href="https://www.xodox.de/">XODOX</a> </footer> </main> </body> </html>