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


Reply via email to