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
alex.barylski
DevNet Evangelist
Posts: 6267 Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg
Post
by alex.barylski » Sat Nov 26, 2005 12:50 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sat Nov 26, 2005 12:59 pm
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 » Sat Nov 26, 2005 1:04 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sat Nov 26, 2005 1:05 pm
Code: Select all
$regex = '#((?<=/)/)|(/$)|(^/)#';
echo preg_replace($regex, '', '/usr/bin//foo//bar/something/');
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sat Nov 26, 2005 1:08 pm
Ooops
I editted your post rather than quoting it
My bad. Anyway read your last post to see my answer
alex.barylski
DevNet Evangelist
Posts: 6267 Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg
Post
by alex.barylski » Sat Nov 26, 2005 1:16 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sat Nov 26, 2005 1:19 pm
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
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Sat Nov 26, 2005 1:20 pm
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 » Sat Nov 26, 2005 1:31 pm
Wicked cool man...thanks alot it worked