yoda
yoda

Reputation: 10981

PHP - reduce multiple slashes to single slash

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?

Upvotes: 1

Views: 5272

Answers (7)

ganji
ganji

Reputation: 844

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);

Upvotes: 6

Kerem
Kerem

Reputation: 11576

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/');

Upvotes: 1

Powerlord
Powerlord

Reputation: 88796

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';

Upvotes: 2

Manrico Corazzi
Manrico Corazzi

Reputation: 11371

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

Upvotes: 1

user187291
user187291

Reputation: 53940

you can combine all three alternatives into one regexp

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

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

Upvotes: 1

Select0r
Select0r

Reputation: 12638

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).

Upvotes: 2

Notinlist
Notinlist

Reputation: 16640

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,"/");

Upvotes: 7

Related Questions