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/
No regex needed, rather use ltrim()
with /.
. Like this:
echo "/".ltrim("../../../somefile/here/", "/.");
This outputs:
/somefile/here/
You could use the realpath() function PHP provides. This requires the file to exist, however.
If I understood you correctly:
$path = "/".str_replace("../","","../../../somefile/here/");
This should work:
<?php
echo "/".preg_replace('/\.\.\/+/',"","../../../somefile/here/")
?>
You can test it here.
You could try :
<?php
$str = '../../../somefile/here/';
$str = preg_replace('~(?:\.\./)+~', '/', $str);
echo $str,"\n";
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With