Strip path slashes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Strip path slashes

Post by alex.barylski »

Can someone show me using regex how I would strip ONLY trailing, pre-ceeding and duplicate slashes from a path???

so for example:

/www/html/docs//test

Would need to become:

www/html/docs/test

Also...are path slashes always '/' or are they ever '\'?

Is this a Windows or Linux issue? How can I tell programtically so I specify the right seperator?

Thanks :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Windows uses \ but everything else uses /

PHP, however accepts the / version on windows systems just for compatibility reasons.

This regex might help:

Code: Select all

$regex = '#((?<=/)/)|(/$)#';
echo preg_replace($regex, '', '/usr/bin//foo//bar/something/');

/*
 /usr/bin/foo/bar/something
 */
;)

That weird (?<=/) is a postive lookbehind if you wanted to research it.

For windows the regex is '#((?<=\\)\\)|(\\$)#' and those can be done in function form like this for example:

Code: Select all

function drop_path_slashes($path)
{

    if strpos('/', $path)
    {
        $regex = '#((?<=/)/)|(/$)#'; //As far as I know /  is illegal in windows paths.
    }
    else
    {
        $regex = '#((?<=\\)\\)|(\\$)#';
    }
    return preg_replace($regex, '', $path);

}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

d11wtq | This post was made by myself in error because I stupidly clicked the wrong button ;) and in the process removed the original poster's post 8O
Update I just tried it and got this error:
Warning: ereg_replace(): REG_BADRPT

??? :(
preg_...() not ereg(). ereg is nowhere near as smart as preg_ and I personally dislike it if there's perl style regex available ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$regex = '#((?<=/)/)|(/$)|(^/)#';
echo preg_replace($regex, '', '/usr/bin//foo//bar/something/');
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ooops :oops: I editted your post rather than quoting it :? My bad. Anyway read your last post to see my answer :P
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

d11wtq wrote:Ooops :oops: I editted your post rather than quoting it :? My bad. Anyway read your last post to see my answer :P
I still get that REG_BADRPT error unless I slash the ? inside the lookahead as in:

Code: Select all

echo ereg_replace('#((\?<=/)/)|(/$)|(^/)#', '', $dir_path);
But when I add that slash...it appears nothing is done to the $dir_path variable...

What are the # for anyways? Are they a substitute for / slashes?

Thanks for the help :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hockey wrote:
d11wtq wrote:Ooops :oops: I editted your post rather than quoting it :? My bad. Anyway read your last post to see my answer :P
I still get that REG_BADRPT error unless I slash the ? inside the lookahead as in:

Code: Select all

echo ereg_replace('#((\?<=/)/)|(/$)|(^/)#', '', $dir_path);
But when I add that slash...it appears nothing is done to the $dir_path variable...

What are the # for anyways? Are they a substitute for / slashes?

Thanks for the help :)
You're still using ereg :? Use preg_replace(). Cop & Paste my code ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

If this is a directory on your server, no need to use regex.. use realpath()
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Wicked cool man...thanks alot it worked :)
Post Reply