在 PHP 中列出名称以数字开头的目录

发布于 2024-09-08 11:14:24 字数 181 浏览 1 评论 0原文

以下代码为我提供了所有目录:

print_r(glob('*',GLOB_ONLYDIR));

但我只需要以数字开头的目录(版本号 3.0.4、3.0.5 等)。

我正在考虑使用 foreach 循环和一些测试条件。

还有其他方法可以做到这一点吗?

The following code give me all the directories:

print_r(glob('*',GLOB_ONLYDIR));

But I need only the ones that start with a digit (version numbers 3.0.4, 3.0.5 etc).

I was thinking of using a foreach loop and some test conditions.

Is there another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自在安然 2024-09-15 11:14:24

您可以使用简单的类似正则表达式的构造:

print_r(glob("[0-9]*", GLOB_ONLYDIR));

给定这些目录:

12test
1test
2test
test

上面的 glob 返回:

Array
(
    [0] => 12test
    [1] => 1test
    [2] => 2test
)

如果您愿意,可以进一步缩小范围:

print_r(glob("[0-9]\.[0-9]\.[0-9]*", GLOB_ONLYDIR));

给定这些目录:

3.0.4.Test
3.0.Test

上面的 glob 返回:

Array
(
    [0] => 3.0.4.Test
)

您可能会发现这很有用:
PHP 中文件匹配的 Glob 模式

You can use simple regular-expression-like constructs:

print_r(glob("[0-9]*", GLOB_ONLYDIR));

Given these directories:

12test
1test
2test
test

The above glob returns:

Array
(
    [0] => 12test
    [1] => 1test
    [2] => 2test
)

You can narrow it down further if you like:

print_r(glob("[0-9]\.[0-9]\.[0-9]*", GLOB_ONLYDIR));

Given these directories:

3.0.4.Test
3.0.Test

The above glob returns:

Array
(
    [0] => 3.0.4.Test
)

You might find this useful:
Glob Patterns for File Matching in PHP

百合的盛世恋 2024-09-15 11:14:24

我建议检查 php 中的目录交互器而不是 glob

http://php.net /manual/en/class.directoryiterator.php

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        // check if $fileinfo->getFilename() matches your criteria
    }
}
?>

正如@Mike所说,您也可以使用正则表达式交互器。 http://php.net/regexiterator

I would recommend checking out the directory interator in php rather than glob

http://php.net/manual/en/class.directoryiterator.php

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        // check if $fileinfo->getFilename() matches your criteria
    }
}
?>

As @Mike says, you can use the Regex Interator too. http://php.net/regexiterator

潜移默化 2024-09-15 11:14:24
foreach(glob('*',GLOB_ONLYDIR) as $directoryname)
{
    if (strstr('0123456789',substr($directoryname,0,1))!="")
    {
         //$directoryname starts with a digit
    }
}
foreach(glob('*',GLOB_ONLYDIR) as $directoryname)
{
    if (strstr('0123456789',substr($directoryname,0,1))!="")
    {
         //$directoryname starts with a digit
    }
}
野鹿林 2024-09-15 11:14:24

如果要检索与 3.0.4、3.0.5 或 3.0.10 匹配的所有目录,
您可以使用以下代码。

$dirs = glob("[0-9]+\.[0-9]+\.[0-9]+", GLOB_ONLYDIR);

If you want to retrieve all directories that matches 3.0.4, or 3.0.5 or 3.0.10 ,
you can use the following code.

$dirs = glob("[0-9]+\.[0-9]+\.[0-9]+", GLOB_ONLYDIR);
梦途 2024-09-15 11:14:24

或者通过使用正则表达式过滤器迭代器,如果您需要真正的正则表达式功能(因为 GLOB 没有),

$dir_iterator = new DirectoryIterator('./');
$regex = '#^\d.*

我想它可以做得更短一些,并且可以得到增强。不管怎样,很高兴今天学到了一种新的目录过滤方法;)

; // something really basic for now $iterator = new RegexIterator($dir_iterator, $regex); foreach ($iterator as $dir_object) { if ($dir_object->isDir()) { // Just do something with it. echo $dir_object->getPathname()."<br/>\n"; } }

我想它可以做得更短一些,并且可以得到增强。不管怎样,很高兴今天学到了一种新的目录过滤方法;)

Or by using a Regex Filter iterator, in case you need real regex power ('cos GLOB hasn't)

$dir_iterator = new DirectoryIterator('./');
$regex = '#^\d.*

I imagine it could be done a little bit shorter, and it could be enhanced. Anyway, glad to have learned a new approach towards directory filtering today ;)

; // something really basic for now $iterator = new RegexIterator($dir_iterator, $regex); foreach ($iterator as $dir_object) { if ($dir_object->isDir()) { // Just do something with it. echo $dir_object->getPathname()."<br/>\n"; } }

I imagine it could be done a little bit shorter, and it could be enhanced. Anyway, glad to have learned a new approach towards directory filtering today ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文