It seems like an easy problem to solve, but It's not as easy as it seems. I have this string in PHP:
////%postname%/
This is a URL and I never want more than one slash in a row. I never want to remove the slashes completely.
This is how it should look like:
/%postname%/
Because the structure could look different I need a clever preg replace regexp, I think. It need to work with URLS like this:
////%postname%//mytest/test///testing
which should be converted to this:
/%postname%/mytest/test/testing
Here you go:
$str = preg_replace('~/+~', '/', $str);
Or:
$str = preg_replace('~//+~', '/', $str);
Or even:
$str = preg_replace('~/{2,}~', '/', $str);
A simple str_replace()
will also do the trick (if there are no more than two consecutive slashes):
$str = str_replace('//', '/', $str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With