计算出两个文件的相对路径 如 $a = ‘/a/b/c/d/e.php’; $b = ‘/a/b/12/34/c.php’;计算出 c.php相对于e.php的相对路径应该是../../c/d用PHP怎么来写?请高手帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php';function getRelative($a,$b) { $arr = explode("/",$a); $brr = explode("/",$b); $c = count($arr)-2; $d = count($brr)-2; //之所以减二,一个是不在后面的文件名, //一个是数组的索引是从0开始的,比数组中的第一维的个数要小一 $e = ($c>$d) ? $c:$d; $str1 = $str2 = ''; for ($j=0;$j<=$e;$j++) { $cur_a = isset($arr[$j]) ? $arr[$j] : ''; $cur_b = isset($brr[$j]) ? $brr[$j] : ''; if ($cur_a == $cur_b) { continue; } else { if ($j <= $c) { $str1.='/'.$cur_a; } if ($j <= $d ) { $str2.="../"; } } } return $str2.substr($str1,1,strlen($str1)); }echo getRelative($a,$b);?>网上答案很多 $a = '/a/b/c/d/e.php';$b = '/a/b/12/34/c.php';$a1 = split('/', trim(dirname($a), '/'));$b1 = split('/', trim(dirname($b), '/'));foreach($a1 as $i=>$v) { if($v == $b1[$i]) { unset($a1[$i]); unset($b1[$i]); }else break;}$p = str_repeat('../', count($b1)) . join('/', $a1);echo $p; $a = '/a/b/c/d/e.php';$b = '/a/b/12/34/c.php';getpathinfo($a, $b);function getpathinfo( $a, $b ) { $a2array = explode('/', $a); $b2array = explode('/', $b); $pathinfo = ''; $fileinfo = ''; for( $i = 0; $i <= count($a2array) - 2; $i++ ) { $pathinfo .= $a2array[$i] != $b2array[$i] ? '../' : ''; if($a2array[$i] != $b2array[$i]) $fileinfo[] = $b2array[$i]; } print_R($pathinfo.implode('/', $fileinfo));} PHP开发的离线下载,大家帮忙测试一下 PHP乱码问题 PHPEclipse 使用问题讨教 紧急求救:论坛用户整合的问题 传值问题 模板(smarty)替换内容里有{xxx}怎么办? php如何添加js? 有没有上传文件的源文件?php 好问题 如何用GET方法处理内容是中文的表单 PHP一个小错误 老师进来帮忙分析下 关于系统优化...求救。。
<?php
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
function getRelative($a,$b) {
$arr = explode("/",$a);
$brr = explode("/",$b);
$c = count($arr)-2;
$d = count($brr)-2;
//之所以减二,一个是不在后面的文件名,
//一个是数组的索引是从0开始的,比数组中的第一维的个数要小一
$e = ($c>$d) ? $c:$d;
$str1 = $str2 = '';
for ($j=0;$j<=$e;$j++) {
$cur_a = isset($arr[$j]) ? $arr[$j] : '';
$cur_b = isset($brr[$j]) ? $brr[$j] : '';
if ($cur_a == $cur_b) {
continue;
} else {
if ($j <= $c)
{
$str1.='/'.$cur_a;
}
if ($j <= $d )
{
$str2.="../";
}
}
}
return $str2.substr($str1,1,strlen($str1));
}
echo getRelative($a,$b);
?>网上答案很多
$b = '/a/b/12/34/c.php';$a1 = split('/', trim(dirname($a), '/'));
$b1 = split('/', trim(dirname($b), '/'));foreach($a1 as $i=>$v) {
if($v == $b1[$i]) {
unset($a1[$i]);
unset($b1[$i]);
}else break;
}$p = str_repeat('../', count($b1)) . join('/', $a1);echo $p;
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
getpathinfo($a, $b);
function getpathinfo( $a, $b ) {
$a2array = explode('/', $a);
$b2array = explode('/', $b);
$pathinfo = '';
$fileinfo = '';
for( $i = 0; $i <= count($a2array) - 2; $i++ ) {
$pathinfo .= $a2array[$i] != $b2array[$i] ? '../' : '';
if($a2array[$i] != $b2array[$i]) $fileinfo[] = $b2array[$i];
}
print_R($pathinfo.implode('/', $fileinfo));
}