tags:

views:

48

answers:

6

how could I remove the trailing slashes and dots from a non root-relative path.

For instance, ../../../somefile/here/ (independently on how deep it is) so I just get /somefile/here/

+2  A: 

No regex needed, rather use ltrim() with /. . Like this:

 echo "/".ltrim("../../../somefile/here/", "/.");

This outputs:

 /somefile/here/
shamittomar
Note that the leading slash is significant — it invariably refers to the root directory.
BoltClock
Yes, that's why I appended it specially. User can remove it if needed and can still successfully strip all `/` and `.`
shamittomar
What about `/.foo`?
Gumbo
@Gumbo, that would display `/foo` stripping *slashes and dots* as required by the OP :)
shamittomar
@shamittomar: Gumbo has a good point actually. `/.foo` is not the same as `/foo`.
BoltClock
@BoltClock, yes I agree, they are definitely not same. But, even `../../../somefile/here/` and `/somefile/here/` are not same too (other than a case where it is in the directory which is in right position). So, I just answered the OP's question of what he wanted (*to strip `/` and `.`*) and left the reasoning and actual implementation and results to the OP and its web application.
shamittomar
+1  A: 

You could use the realpath() function PHP provides. This requires the file to exist, however.

Brad F Jacobs
@Whoever downvoted, mind elaborating please? Reading the manual, this does what he wants, given the file exists.
Brad F Jacobs
AND it will do the right thing relative to the .. 's. Something programmers OFTEN miss is a ".." on a *nix system might lead through a symlink, so the ..'s might actually move it to an entirely different part of the disk.
Robert P
A: 

If I understood you correctly:

$path = "/".str_replace("../","","../../../somefile/here/");  
fatnjazzy
This would **fail** on `/../../../somefile/here/` and will return `//somefile/here`
shamittomar
A: 

(\.*/)*(?<capturegroup>.*)

The first group matches some number of dots followed by a slash, an unlimited number of times; the second group is the one you're interested in. This will strip your leading slash, so prepend a slash.

Beware that this is doing absolutely no verification that your leading string of slashes and periods isn't something patently stupid. However, it won't strip leading dots off your path, like the obvious ([./])* pattern for the first group would; it finds the longest string of dots and slashes that ends with a slash, so it won't hurt your real path if it begins with a dot.

Be aware that the obvious "/." ltrim() strategy will strip leading dots from directory names, which is Bad if your first directory has one- entirely plausible, since leading dots are used for hidden directories.

Kistaro Windrider
A: 

This should work:

<?php
echo "/".preg_replace('/\.\.\/+/',"","../../../somefile/here/")
?>

You can test it here.

Vikash
+1  A: 

You could try :

<?php
$str = '../../../somefile/here/';
$str = preg_replace('~(?:\.\./)+~', '/', $str);
echo $str,"\n";
?>
M42