Helpful Information
 
 
Category: Regex Programming
Replacing multiple slashes with single slashes in urls/paths

Hello all,

I'm trying to clean up some URL's that I have in my database and I am trying to work out a way to use one of PHP's regex functions to strip out any double (or more) slashes in paths and urls.

I'm generally quite good with regex but I'm having difficulty searching for a '/' as it seems to keep throwing an error with that character.

Thanks as always!

Matt.

It shouldn't throw an error unless you're using it as your delimiter:
$new = preg_replace("#/+#", "/", $old);-Dan

It shouldn't throw an error unless you're using it as your delimiter:
$new = preg_replace("#/+#", "/", $old);-Dan

I had a feeling it may have had something to do with using '/' as a delimiter. I should probably put some time asside to read up on the fundamentals of regex!

Your sample code works perfectly, thankyou very much!

Matt.










privacy (GDPR)