php计算文件的相对路径函数 php相对路径phpfunction 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 a = 'c/e.php';$b = '/12/34/c.php';echo getRelativePath($a, $b); // ../../e.php 这样可能好些$a = '/c/e.php';$b = '/12/34/c.php';// 计算出 $b 相对于 $a 的相对路径应该是 ../../c/e.phpfunction getRelativePath($a, $b) { $c['\\'] = '/'; $a = explode('/', trim(strtr($a, $c), '/')); $b = explode('/', trim(strtr($b, $c), '/')); $t = array_intersect_assoc($a, $b); $r = array_merge(array_fill(0, count($b) - count($t) - 1, '..'), array_slice($a, count($t))); return join('/', $r);}echo getRelativePath($a, $b); $a = '/home/web/lib/img/cache.php';$b = '/home/web/api/img/show.php';这样就不行了。 改了一下/** 计算path1 相对于 path2 的路径,即在path2引用paht1的相对路径 * @param String $path1 * @param String $path2 * @return String */ function getRelativePath($path1, $path2){ $arr1 = explode('/', $path1); $arr2 = explode('/', $path2); // 获取相同路径的部分 $intersection = array_intersect_assoc($arr1, $arr2); $depth = 0; for($i=0,$len=count($intersection); $i<$len; $i++){ if(!isset($intersection[$i])){ $depth = $i; break; } } // 将path2的/ 转为 ../,path1获取后面的部分,然后合拼 $tmp = array_merge(array_fill(0, count($arr2)-$depth-1, '..'), array_slice($arr1, $depth)); $relativePath = implode('/', $tmp); return $relativePath; } function getRelativePath($a, $b) { $c['\\'] = '/'; $a = explode('/', trim(strtr($a, $c), '/')); $b = explode('/', trim(strtr($b, $c), '/')); $t = 0; foreach($a as $k=>$v) { if($b[$k] == $v) $t++; else break; } $r = array_merge(array_fill(0, count($b) - $t, '..'), array_slice($a, $t)); return join('/', $r);}$a = '/home/web/lib/img/cache.php';$b = '/home/web/api/img/show.php';echo getRelativePath($a, $b); php与mysql简单分页问题? 菜鸟求助:php分页问题 怎样在php里弹出提示框? 问个数据结构的问题,哈夫曼树 apache将post和get方法获得的值存在哪?PHP5又是如何获得这些值的? 寻php兼职 谁有简单的Weblog源程序?只要有数据库和日期更新的哪种,急用!我参考一下!!!!!!!!! 急!忘高人给思路? 问:怎么才能实现点击一下按钮就能转到令一个页面? 求教php随机数 哪位高人帮忙把以此JAVA程序改成PHP,谢谢! iframe如何与另一个iframe链接
$b = '/12/34/c.php';
echo getRelativePath($a, $b); // ../../e.php
$b = '/12/34/c.php';
// 计算出 $b 相对于 $a 的相对路径应该是 ../../c/e.php
function getRelativePath($a, $b) {
$c['\\'] = '/';
$a = explode('/', trim(strtr($a, $c), '/'));
$b = explode('/', trim(strtr($b, $c), '/'));
$t = array_intersect_assoc($a, $b);
$r = array_merge(array_fill(0, count($b) - count($t) - 1, '..'), array_slice($a, count($t)));
return join('/', $r);
}
echo getRelativePath($a, $b);
$a = '/home/web/lib/img/cache.php';
$b = '/home/web/api/img/show.php';这样就不行了。
* @param String $path1
* @param String $path2
* @return String
*/
function getRelativePath($path1, $path2){
$arr1 = explode('/', $path1);
$arr2 = explode('/', $path2);
// 获取相同路径的部分
$intersection = array_intersect_assoc($arr1, $arr2);
$depth = 0;
for($i=0,$len=count($intersection); $i<$len; $i++){
if(!isset($intersection[$i])){
$depth = $i;
break;
}
}
// 将path2的/ 转为 ../,path1获取后面的部分,然后合拼
$tmp = array_merge(array_fill(0, count($arr2)-$depth-1, '..'), array_slice($arr1, $depth));
$relativePath = implode('/', $tmp);
return $relativePath;
}
$c['\\'] = '/';
$a = explode('/', trim(strtr($a, $c), '/'));
$b = explode('/', trim(strtr($b, $c), '/'));
$t = 0;
foreach($a as $k=>$v) {
if($b[$k] == $v) $t++;
else break;
}
$r = array_merge(array_fill(0, count($b) - $t, '..'), array_slice($a, $t));
return join('/', $r);
}$a = '/home/web/lib/img/cache.php';
$b = '/home/web/api/img/show.php';echo getRelativePath($a, $b);