写一个函数,算出两个文件的相对路径
程序员文章站
2022-05-09 23:05:21
...
结果:
$a = E:\a\b\b\c\d\e\a.php
$b = E:\a\b\b\c\d\e\f.php
.\f.php
getRelativePath耗时:0.000607
.\f.php
getRelativePath2耗时:0.000471
---------------------------------------
$a = E:\a\b\b\c\d\e\a.php
$b = E:\f.php
..\..\..\..\..\..\f.php
getRelativePath耗时:0.000344
..\..\..\..\..\..\f.php
getRelativePath2耗时:0.000341
---------------------------------------
$a = E:\a\b\f.php
$b = E:\a\b\f.php
同一文件
getRelativePath耗时:0.000312
同一文件
getRelativePath2耗时:0.00027
---------------------------------------
$a = a\c\f.php
$b = a\b\f.php
..\b\f.php
getRelativePath耗时:0.000435
..\b\f.php
getRelativePath2耗时:0.00046
---------------------------------------
$a = E:\.\a\.\.\\\.\.\f.php
$b = E:\\a\.\..\b\f.php
..\b\f.php
getRelativePath耗时:0.000542
..\b\f.php
getRelativePath2耗时:7.90000000001E-5
---------------------------------------
$b = 'E:\a\b\b\c\d\e\f.php';
$a = 'E:\a\b\b\c\d\e\a.php';
test($b,$a);
$b = 'E:\f.php';
$a = 'E:\a\b\b\c\d\e\a.php';
test($b,$a);
$b = 'E:\a\b\f.php';
$a = 'E:\a\b\f.php';
test($b,$a);
$b = 'a\b\f.php';
$a = 'a\c\f.php';
test($b,$a);
$b = 'E:\\\\a\.\..\b\f.php'; //实际是E:\b\f.php
$a = 'E:\.\a\.\.\\\\\.\\.\f.php';//实际是E:\a\f.php
test($b,$a);
//写一个函数,数出二个文件的相对路径。
function getRelativePath($variant,$variant2, $debug = false, $ds = DIRECTORY_SEPARATOR){//$ds 目录分隔符
$pth1 = null;
$pth2 = null;
$pthStr1 = null;
$pthStr2 = null;
$tmp = array();
$return = null;
$do = $debug == true ? true : is_file($variant) && is_file($variant2);//分别判断路径下面的文件是不是存在
if($do){
$rpth1 = realpath($variant);
$rpth2 = realpath($variant2);
$rpth1 = $rpth1 == false ? $variant : $rpth1;
$rpth2 = $rpth2 == false ? $variant2 : $rpth2;
$len1 = count($pth1 = explode($ds, $pthStr1 = dirname($rpth1)));
$len2 = count($pth2 = explode($ds, $pthStr2 = dirname($rpth2)));
if(strpos($pth1[0], ':') == 1 && strpos($pth2[0], ':') == 1 && $pth1[0] != $pth2[0]){
$return = $rpth1;
}else{
$len3 = count($same = array_intersect_assoc($pth1,$pth2));
if($len3 == $len1 && $len3 == $len2){
$return = basename($rpth1) == basename($rpth2) ? '同一文件' : '.'.$ds.basename($rpth1);
}else{
$maxlen = max($len1, $len2);
for($i=1;$i<$maxlen;$i++){
if($pth1[$i] != $pth2[$i]){
if(isset($pth1[$i]) && !empty($pth1[$i]))$tmp[] = $pth1[$i];
$pathe .= isset($pth2[$i]) && !empty($pth2[$len2-1]) ? '..'.$ds : (strpos($pathe, $ds) > 0 ? '' : '.'.$ds);
}
}
$tmp[] = basename($rpth1);
$return = $pathe.implode($ds, $tmp);
}
}
}else{
$return = '路径不合法!';
}
return $return;
}
function getRelativePath2($variant,$variant2, $debug = false, $ds = DIRECTORY_SEPARATOR){//$ds 目录分隔符
$pth1 = null;
$pth2 = null;
$pthStr1 = null;
$pthStr2 = null;
$tmp = array();
$return = null;
$do = $debug == true ? true : is_file($variant) && is_file($variant2);//分别判断路径下面的文件是不是存在
if($do){
$rpth1 = realpath($variant);
$rpth2 = realpath($variant2);
$rpth1 = $rpth1 == false ? $variant : $rpth1;
$rpth2 = $rpth2 == false ? $variant2 : $rpth2;
$len1 = count($pth1 = explode($ds, $pthStr1 = dirname($rpth1)));
$len2 = count($pth2 = explode($ds, $pthStr2 = dirname($rpth2)));
if(strpos($pth1[0], ':') == 1 && strpos($pth2[0], ':') == 1 && $pth1[0] != $pth2[0]){
$return = $rpth1;
}else{
$len3 = count($same = array_intersect_assoc($pth1,$pth2));
if($len3 == $len1 && $len3 == $len2){
$return = basename($rpth1) == basename($rpth2) ? '同一文件' : '.'.$ds.basename($rpth1);
}else{
$minlen = min($len1, $len2);
$sameIndex = $minlen;
$samepartlen = 0;
for($i=0;$i<$minlen;$i++){
if($pth1[$i] != $pth2[$i]){
$sameIndex = $i;
break;
}else{
$samepartlen += strlen($pth1[$i].$ds);
}
}
$tmp = isset($pth2[$len2-1]) && !empty($pth2[$len2-1]) ? array_fill(0,$len2 - $sameIndex, '..') : array('.');
$tmp[] = substr($rpth1, $samepartlen);
$return = $pathe.implode($ds, $tmp);
}
}
}else{
$return = '路径不合法!';
}
return $return;
}
function test($b,$a){
echo '$a = '.$a;
echo '<br>$b = '.$b.'<br>';
$t0 = microtime();
echo getRelativePath($b,$a,true);
echo '<br>getRelativePath耗时:'.(microtime()-$t0).'<br>';
$t1 = microtime();
echo getRelativePath2($b,$a,true);
echo '<br>getRelativePath2耗时:'.(microtime()-$t1);
echo '<br><br>---------------------------------------<br><br>';
}
转载于:https://my.oschina.net/7day/blog/85268
上一篇: 崇祯为什么会用如此简陋的祭品供奉朱元璋?
下一篇: 妲己发明了一样东西,现在家家户户都在用