如何从绝对路径和某个给定路径获取相对文件名?
例如:
foo('/a/b/c/1.txt', '/a/d/e'); // '../../b/c/1.txt'
foo('/a/b/../d/1.txt', '/a/d/e'); // '../c/1.txt'
这有一些原生功能吗?
我的想法,如果没有:
realpath
替换,因为文件可能不存在。 example 对于这项常见任务,手动方式看起来太重了。
答案 0 :(得分:1)
目前,我假设没有任何本机实现,并希望分享我对此的实现。
/**
* realpath analog without filesystem check
*/
function str_normalize_path(string $path, string $sep = DIRECTORY_SEPARATOR): string {
$parts = array_filter(explode($sep, $path));
$stack = [];
foreach ($parts as $part) {
switch($part) {
case '.': break;
case '..': array_pop($stack); break; // excess '..' just ignored by array_pop([]) silency
default: array_push($stack, $part);
}
}
return implode($sep, $stack);
}
function str_relative_path(string $absolute, string $base, string $sep = DIRECTORY_SEPARATOR) {
$absolute = str_normalize_path($absolute);
$base = str_normalize_path($base);
// find common prefix
$prefix_len = 0;
for ($i = 0; $i < min(mb_strlen($absolute), mb_strlen($base)); ++$i) {
if ($absolute[$i] !== $base[$i]) break;
$prefix_len++;
}
// cut common prefix
if ($prefix_len > 0) {
$absolute = mb_substr($absolute, $prefix_len);
$base = mb_substr($base, $prefix_len);
}
// put '..'s for exit to closest common path
$base_length = count(explode($sep, $base));
$relative_parts = explode($sep, $absolute);
while($base_length-->0) array_unshift($relative_parts, '..');
return implode($sep, $relative_parts);
}
$abs = '/a/b/../fk1/fk2/../.././d//proj42/1.txt';
$base = '/a/d/fk/../e/f';
echo str_relative_path($abs, $base); // ../../proj42/1.txt