views:

315

answers:

5

Hi,

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?

+5  A: 

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,"/");
Notinlist
Or specially ltrim() or rtrim().
Notinlist
thanks, trim saved my day :D
yoda
str_replace won't handle three slashes in a row
stereofrog
no, but my script combined with trim, handled all the slashes my fingers could type untill I got tired, so .. :)
yoda
yes, but there's no point using two functions in place of one
stereofrog
+2  A: 

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

Select0r
+1  A: 

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

Manrico Corazzi
+1  A: 

you can combine all three alternatives into one regexp

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

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

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';
R. Bemrose