Read some of the answers to this question on the Internet, but most of them are based on current needs, such as
$a = '/a/b/c/d/e.php ';
$b = '/a/d/12/34/c.php ';
GetPath ($a, $b); Publicfunction GetPath ($a, $b) {$aarr= Explode ('/', $a); $barr= Explode ('/', $b); $count= Count ($barr)-2; $pathinfo="'; for($i =1; $i <= $count; $i + +){ if($aarr [$i] = =$barr [$i]) {$pathinfo.='.. /'; }Else{$pathinfo.= $barr [$i].'/'; }} echo $pathinfo;}
If you change
$a = '/a/b/c/d/e.php ';
$b = '/a/b/c/34/c.php ';
Then there will be problems, so I wrote a simple
function GetPath ($a, $b) {$aarr= Explode ('/', $a); $barr= Explode ('/', $b); $flag=true; $count= Count ($barr)-2; $pathinfo="'; for($i =1; $i <= $count; $i + +){ if($flag && $aarr [$i]! =$barr [$i]) {$pathinfo.= Str_repeat ('.. /', $count-$i +1). $aarr [$i].'/'; $flag=false; }elseif (!$flag) {$pathinfo.= $aarr [$i].'/'; } } returnEmpty ($pathinfo)?'./': $pathinfo; } Echo GetPath ($a, $b);
PHP for two file relative path