Question

I have a regular expression that I use to reduce multiple slashes to single slashes. The purpose is to read a url that is previously converted to a human readable link using mod_rewrite in apache, like this :

http://www.website.com/about/me

This works :

$uri = 'about//me';
$uri = preg_replace('#//+#', '/', $uri);
echo $uri; // echoes 'about/me'

This doesn't work :

$uri = '/about//me';
$uri = preg_replace('#//+#', '/', $uri);
echo $uri; // echoes '/about/me'

I need to be able to work with each url parameter alone, but in the second example, if I explode the trailling slash, it would return me 3 segments instead of 2 segments. I can verify in PHP if any if the parameters is empty, but as I'm using that regular expression, it would be nice if the regular expression already take care of that for me, so that I don't need to worry about segment validation.

Any thoughts?

Was it helpful?

Solution

str_replace may be faster in this case

$uri = str_replace("//","/",$uri)

Secondly: use trim: http://hu.php.net/manual/en/function.trim.php

$uri = trim($uri,"/");

OTHER TIPS

How about running a second replace on $uri?

$uri = preg_replace('#^/#', '', $uri);

That way a trailing slash is removed. Doing it all in one preg_replace beats me :) Using ltrim could also be a way to go (probably even faster).

I need to be able to work with each url parameter alone, but in the second example, if I explode the trailling slash, it would return me 3 segments instead of 2 segments.

One fix for this is to use preg_split with the third argument set to PREG_SPLIT_NO_EMPTY:

$uri = '/about//me';
$uri_segments = preg_split('#/#', $uri, PREG_SPLIT_NO_EMPTY);
// $uri_segments[0] == 'about';
// $uri_segments[1] == 'me';

This converts double slashes in a string to a single slash, but the advantage of this code is that the slashes in the protocol portion of the string (http://) are kept.

preg_replace("#(^|[^:])//+#", "\\1/", $str);

you can combine all three alternatives into one regexp

$urls = array(
   'about/me',
   '/about//me',
   '/about///me/',
   '////about///me//'
);

print_r(
     preg_replace('~^/+|/+$|/(?=/)~', '', $urls)
);

You may split the string via preg_split instead, skipping the sanitizing altogether. You still have to deal with the empty chunks, though.

Late but all these methods will remove http:// slashes too, but this.

function to_single_slashes($input) {
    return preg_replace('~(^|[^:])//+~', '\\1/', $input);
}

# out: http://localhost/lorem-ipsum/123/456/
print to_single_slashes('http:///////localhost////lorem-ipsum/123/////456/');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top