Page 1 of 1
Strip path slashes
Posted: Sat Nov 26, 2005 12:50 pm
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

Posted: Sat Nov 26, 2005 12:59 pm
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);
}
Posted: Sat Nov 26, 2005 1:04 pm
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
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

Posted: Sat Nov 26, 2005 1:05 pm
by Chris Corbyn
Code: Select all
$regex = '#((?<=/)/)|(/$)|(^/)#';
echo preg_replace($regex, '', '/usr/bin//foo//bar/something/');
Posted: Sat Nov 26, 2005 1:08 pm
by Chris Corbyn
Ooops

I editted your post rather than quoting it

My bad. Anyway read your last post to see my answer

Posted: Sat Nov 26, 2005 1:16 pm
by alex.barylski
d11wtq wrote:Ooops

I editted your post rather than quoting it

My bad. Anyway read your last post to see my answer

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

Posted: Sat Nov 26, 2005 1:19 pm
by Chris Corbyn
Hockey wrote:d11wtq wrote:Ooops

I editted your post rather than quoting it

My bad. Anyway read your last post to see my answer

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

Posted: Sat Nov 26, 2005 1:20 pm
by Jenk
If this is a directory on your server, no need to use regex.. use
realpath()
Posted: Sat Nov 26, 2005 1:31 pm
by alex.barylski
Wicked cool man...thanks alot it worked
