在PHP中列出名称以数字开头的目录 [英] Listing directories in PHP where the name starts with a digit

查看:110
本文介绍了在PHP中列出名称以数字开头的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码给我所有目录:

The following code give me all the directories:

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

但是我只需要一个数字(版本号3.0.4,3.0.5等等)。

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

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

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

有另一种方法吗?

推荐答案

使用简单的正则表达式结构:

You can use simple regular-expression-like constructs:

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

给定这些目录:

12test
1test
2test
test

以上glob返回:

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));

给定这些目录:

3.0.4.Test
3.0.Test

以上glob返回:

Array
(
    [0] => 3.0.4.Test
)

您可能会发现这有用:

用于PHP中文件匹配的Glob模式

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

这篇关于在PHP中列出名称以数字开头的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆