From: Al on 15 Mar 2010 15:03 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.
From: Ashley Sheridan on 15 Mar 2010 15:11 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
From: Ashley Sheridan on 15 Mar 2010 15:30 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
From: james stojan on 15 Mar 2010 16:09 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 <ash(a)ashleysheridan.co.uk>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 > > >
From: Al on 15 Mar 2010 16:17 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.
|
Next
|
Last
Pages: 1 2 Prev: long polling solution for LAMP with limited privilege Next: how to get the local time |