从列表中获取最近的目录

时间:2012-07-28 21:55:16

标签: php list directory closest

我有一个像这样的dinamic目录:

array(4) {
  [0]=>
  string(34) "C:\www\www\mb\core"
  [1]=>
  string(59) "C:\www\www\mb\core\plugins\enabled\response"
  [2]=>
  string(56) "C:\www\www\mb\core\plugins\enabled\tests"
  [3]=>
  string(52) "C:\www\www\mb\core\templates\default"
}

我还有另一个目录要测试:C:\www\www\mb\core\plugins\enabled\includes

我需要通过一个函数知道哪个目录最接近这个目录。 这里的一切都是未知的和动态的。 我尝试过foreach + strpos +字符串大小,但它变得如此丑陋以至于我停下来并来到这里寻求帮助,但它也没有用。 :-P

提前致谢并抱歉我的英语不好,

维尼

1 个答案:

答案 0 :(得分:2)

好吧,我会完全按你所描述的那样做......

function closest_path($path, $paths) {
    $maxMatch = null;
    $maxMatchLength = 0;

    foreach($paths as $item) {
        if(strlen($item) > $maxMatchLength && strpos($path, $item) === 0) {
            $maxMatch = $item;
            $maxMatchLength = strlen($item);
        }
    }

    return $maxMatch;
}