【问题标题】:recursive directory iterator results: filter out specific characters from resultant paths递归目录迭代器结果:从结果路径中过滤掉特定字符
【发布时间】:2015-01-07 12:42:37
【问题描述】:

我正在尝试以递归方式从指定目录返回所有唯一实例路径。

我正在使用RecursiveDirectoryIterator。我还想省略任何包含“。”的路径实例。在他们中,这就是我遇到麻烦的地方。

这是我要做的测试:

<?php

function test($dir){

    $dirArray = []; // the array to store dirs
    $path = realpath($dir);

    $dirs = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
    $objects = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST);

    // loop through all objects and store names in dirArray[]
    foreach($objects as $name => $object){
        if($object->isDir() && strpos($object->getBasename(), '.') !== true) {
          $dirArray[] = $name;
          echo "test: " . $object->getBasename() . "\n";
        }
    }

    print_r($dirArray);
}

test('/some/dir');

?>

这段代码几乎可以满足我的需要。它返回所有唯一的目录,但包括带有“。”的目录。在路径名中。

【问题讨论】:

  • 您是否有一个存在该问题的示例目录名称?也许像path/to/directory.io/yeah。所以不应该包括这个?
  • 都是 git 的东西,所以/blah/.git/blah/blah

标签: php arrays recursion directory


【解决方案1】:

只需在里面添加另一个检查器,然后尝试使用-&gt;getPathname()

if($object->isDir() && strpos($object->getPathname(), '.') === false) {
    // do some stuff
}

这基本上意味着,如果这是一个目录并且路径名不包含 .

【讨论】:

  • @jml 不,它永远不会与真实相比。它可以是0-nfalse。位置0 - to whatever number(如果存在)。 false 如果没有找到
猜你喜欢
  • 2022-01-26
  • 2017-04-12
  • 2017-08-24
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 2021-07-11
  • 2023-02-01
  • 1970-01-01
相关资源
最近更新 更多