无法获取glob来查找所有不以〜开头的文件

时间:2022-09-01 23:04:40

I have writen the following function to recursively find all files not starting with ~. But it doesn't work. In fact it only seems to find those that do start with ~

我已经编写了以下函数来递归查找所有不以〜开头的文件。但它不起作用。实际上它似乎只能找到那些以〜开头的东西

function rfl($dir) {
    $search=$dir."[^~]*";
    echo "searching for: $search<br/>";
    $search=glob($search);  //find every file and directory not starting with ~.  directories are returned with / afterwards
    $list=array();
    foreach ($search as $file) {
        if (is_dir($file)) {
            $list=array_merge($list,rfl($file.'/'));    //recursively search for valid files in directories
            echo 'D: ' . $file.'<br/>';
        } else {
            $list[]=$file;                          //add files to list
            echo 'F: ' . $file.'<br/>';
        }
    }
    return $list;
}
print_r(rfl('code/'));

returns

回报

searching for: code/[^~]*
searching for: code/~private/[^~]*
D: code/~private
Array ( )

Here is the fixed function using Clayton's answer:

以下是使用Clayton答案的固定函数:

function rfl($dir) {
    $search = preg_grep('!^'.$dir.'[^~]!', glob($dir.'*')); //find every file and directory not starting with ~.  directories are returned with / afterwards
    $list=array();
    foreach ($search as $file) {
        if (is_dir($file)) {
            $list=array_merge($list,rfl($file.'/'));    //recursively search for valid files in directories
        } else {
            $list[]=$file;                          //add files to list
        }
    }
    return $list;
}

1 个解决方案

#1


1  

glob does not seem to really allow negation despite what one of the comments says on PHP's glob documentation.

尽管有一条评论在PHP的glob文档中提到,但glob似乎并没有真正允许否定。

Instead I suggest the following as an alternative:

相反,我建议以下作为替代方案:

$path = '../Sites/';
$results = preg_grep('!^'.preg_quote($path).'[^~]!', glob($path.'*'));
print_r($results);

preg_grep will filter the array to only allow results that do not begin with a tilde. Since glob returns a full path, we need to escape and include the path in the regex pattern.

preg_grep将过滤数组以仅允许不以波浪号开头的结果。由于glob返回一个完整的路径,我们需要转义并包含正则表达式模式中的路径。

Read more here: http://php.net/preg_grep

在这里阅读更多:http://php.net/preg_grep

preg_grep — Return array entries that match the pattern

preg_grep - 返回与模式匹配的数组条目

Usage: array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

用法:数组preg_grep(string $ pattern,array $ input [,int $ flags = 0])

Here is your code updated to include the change:

以下是您的代码更新以包含更改:

function rfl($dir) {
    $search=$dir."[^~]*";
    echo "searching for: $search<br/>";

    $search= preg_grep('!^'.preg_quote($dir).'[^~]!', glob($dir.'*')); //find every file and directory not starting with ~.  directories are returned with / afterwards
    $list=array();
    foreach ($search as $file) {
        if (is_dir($file)) {
            $list=array_merge($list,rfl($file.'/'));    //recursively search for valid files in directories
            echo 'D: ' . $file.'<br/>';
        } else {
            $list[]=$file;                          //add files to list
            echo 'F: ' . $file.'<br/>';
        }
    }
    return $list;
}
print_r(rfl('code/'));

#1


1  

glob does not seem to really allow negation despite what one of the comments says on PHP's glob documentation.

尽管有一条评论在PHP的glob文档中提到,但glob似乎并没有真正允许否定。

Instead I suggest the following as an alternative:

相反,我建议以下作为替代方案:

$path = '../Sites/';
$results = preg_grep('!^'.preg_quote($path).'[^~]!', glob($path.'*'));
print_r($results);

preg_grep will filter the array to only allow results that do not begin with a tilde. Since glob returns a full path, we need to escape and include the path in the regex pattern.

preg_grep将过滤数组以仅允许不以波浪号开头的结果。由于glob返回一个完整的路径,我们需要转义并包含正则表达式模式中的路径。

Read more here: http://php.net/preg_grep

在这里阅读更多:http://php.net/preg_grep

preg_grep — Return array entries that match the pattern

preg_grep - 返回与模式匹配的数组条目

Usage: array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

用法:数组preg_grep(string $ pattern,array $ input [,int $ flags = 0])

Here is your code updated to include the change:

以下是您的代码更新以包含更改:

function rfl($dir) {
    $search=$dir."[^~]*";
    echo "searching for: $search<br/>";

    $search= preg_grep('!^'.preg_quote($dir).'[^~]!', glob($dir.'*')); //find every file and directory not starting with ~.  directories are returned with / afterwards
    $list=array();
    foreach ($search as $file) {
        if (is_dir($file)) {
            $list=array_merge($list,rfl($file.'/'));    //recursively search for valid files in directories
            echo 'D: ' . $file.'<br/>';
        } else {
            $list[]=$file;                          //add files to list
            echo 'F: ' . $file.'<br/>';
        }
    }
    return $list;
}
print_r(rfl('code/'));