计算两个路径的父亲路径

简介:

计算/a/b/c/d/e.php  /a/b/12/34/c.php的父亲路径为/a/b

 

 
  1. <?php  
  2. /*  
  3. *系统环境:windows/linux  
  4. *编译环境:php5/php4  
  5. *输入参数:存放在in.txt,多个参数时空格分隔  
  6.                     参数1是一个路径,用\或者/分隔  
  7.                     参数2是一个路径,用\或者/分隔  
  8.                     参数1,2必须都是绝对路径或者相对路径  
  9.                     例如:/a/b/c/d/e.php  /a/b/12/34/c.php  
  10.                       
  11.                     d:\a\b\c\d\e.php  d:\a\b\12\34/c.php  
  12.                       
  13.                     a/b/c/d/e.php  a/b/12/34/c.php  
  14.     输出:out.txt  
  15. */ 
  16. $params=getParams(2);  
  17. $toParh=trim($params[0]);  
  18. $fromParh=trim($params[1]);  
  19. $toParh=str_replace("\\", "/", $toParh);  
  20. $fromParh=str_replace("\\", "/", $fromParh);  
  21.       
  22. //linux形式  
  23. if(substr($toParh, 0,1)=="/" || substr($fromParh, 0,1)=="/")  
  24. {  
  25.     if(!substr($toParh, 0,1)=="/")  
  26.     {  
  27.         //若是$toParh是绝对路径,而$fromParh不是绝对路径,报错      
  28.         error_msg("两个参数必须都是绝对路径或者相对路径\n");  
  29.     }  
  30.     if(!substr($fromParh, 0,1)=="/")  
  31.     {  
  32.         //若是$toParh是绝对路径,而$fromParh不是绝对路径,报错      
  33.         error_msg("两个参数必须都是绝对路径或者相对路径\n");  
  34.     }  
  35. }  
  36. //windows形式  
  37. if(preg_match("/^[a-z]:/i"$toParh) || preg_match("/^[a-z]:/i"$fromParh))  
  38. {  
  39.     if(!preg_match("/^([a-z]):/i"$toParh,$matchs1))  
  40.     {  
  41.         //若是$toParh是绝对路径,而$fromParh不是绝对路径,报错      
  42.         error_msg("两个参数必须都是绝对路径或者相对路径\n");  
  43.     }  
  44.     if(!preg_match("/^([a-z]):/i"$fromParh,$matchs2))  
  45.     {  
  46.         //若是$toParh是绝对路径,而$fromParh不是绝对路径,报错      
  47.         error_msg("两个参数必须都是绝对路径或者相对路径\n");  
  48.     }  
  49.     if($matchs1[1]!=$matchs2[1])  
  50.     {  
  51.         //若是$toParh和$fromParh不在同一个盘里      
  52.         error_msg("两个参数必须都是在同一个盘里\n");  
  53.     }  
  54. }  
  55.  
  56. echo getRalationPath($toParh,$fromParh);  
  57. function getRalationPath($toParh,$fromParh)  
  58. {  
  59.     $toParh=str_replace("\\", "/", $toParh);  
  60.     $fromParh=str_replace("\\", "/", $fromParh);  
  61.     $topaths=split("/",$toParh);  
  62.     $fromparhs=split("/",$fromParh);  
  63.       
  64.     array_pop($fromparhs);  
  65.     array_pop($topaths);  
  66.       
  67.     $relationPath="";  
  68.     //去掉相同目录后,计算需要往上的目录级数  
  69.     $n=count(array_diff_assoc ($fromparhs,$topaths));  
  70.     for($i=0;$i$n;$i++)  
  71.     {  
  72.         $relationPath.="../";     
  73.     }  
  74.     //去掉相同目录后,需要深入的目录  
  75.     $digdir=array_diff_assoc ($topaths,$fromparhs);  
  76.     $relationPath.=join("/",$digdir);  
  77.     return $relationPath;  
  78. }  
  79.  
  80. /*  
  81.     从in.txt里读取参数  
  82. */ 
  83. function getParams($paramNum)  
  84. {  
  85.     $in=file_get_contents("in.txt");  
  86.     if($in===FALSE){  
  87.         error_msg("cannot read in.txt,please check in.txt exists\n");     
  88.     }  
  89.     $in=preg_replace("/(\s+)/i"" "$in);  
  90.     //多个参数时,按照空格分隔  
  91.     $parms=split(" ",trim($in));  
  92.     if($parms===FALSE)  
  93.     {  
  94.         error_msg("cannot get param from in.txt\n");  
  95.     }  
  96.     if(count($parms) < $paramNum)  
  97.     {  
  98.         error_msg("it needs $paramNum params\n");  
  99.     }  
  100.     return $parms;  
  101. }  
  102.  
  103. /*  
  104.     把结果输出到输出文件里  
  105.     当isClean=true时清空out.txt  
  106. */ 
  107. function output($msg,$isClean=false)  
  108. {  
  109.     if($isClean)  
  110.     {  
  111.     $handle = fopen('out.txt''w');  
  112.     fclose($handle);      
  113.     }  
  114.     error_log($msg."\n", 3, "out.txt");  
  115. }  
  116. /*  
  117.     输入错误信息  
  118.     如果$is_exit表示输入信息后退出  
  119. */ 
  120. function error_msg($msg,$is_exit=true)  
  121. {  
  122.     if($is_exit)  
  123.         die($msg."\n");  
  124.     else 
  125.         echo $msg."\n";  
  126. }  
  127. ?> 

      本文转自yifangyou 51CTO博客,原文链接:     本文转自yifangyou 51CTO博客,原文链接:,如需转载请自行联系原作者

,如需转载请自行联系原作者



相关文章
|
2月前
|
算法 前端开发
判断路径是否相交
判断路径是否相交
20 0
|
6天前
|
算法 机器人 BI
动态规划|【路径问题】|62.不同路径I
动态规划|【路径问题】|62.不同路径I
|
6天前
|
机器人
动态规划|【路径问题】63.不同路径II
动态规划|【路径问题】63.不同路径II
|
2月前
|
测试技术
【树】【图论】【树路径】【深度优先搜索】2867. 统计树中的合法路径数目
【树】【图论】【树路径】【深度优先搜索】2867. 统计树中的合法路径数目
|
3月前
|
C++
二叉树的所有路径(C++)
二叉树的所有路径(C++)
19 1
|
3月前
|
C++
路径总和(C++)
路径总和(C++)
18 1
|
4月前
|
存储 算法 程序员
【算法训练-二叉树 六】【路径和计算】路径总和I、路径总和II、路径总和III、二叉树的最大路径和
【算法训练-二叉树 六】【路径和计算】路径总和I、路径总和II、路径总和III、二叉树的最大路径和
48 0
|
5月前
|
存储 C++
C++二叉树的所有路径
C++二叉树的所有路径
|
5月前
|
机器人
【动态规划算法】不同路径_不同路径升级版
【动态规划算法】不同路径_不同路径升级版
29 0
|
9月前
|
算法 Go