在 PHP 中列出名称以数字开头的目录
以下代码为我提供了所有目录:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用简单的类似正则表达式的构造:
给定这些目录:
上面的 glob 返回:
如果您愿意,可以进一步缩小范围:
给定这些目录:
上面的 glob 返回:
您可能会发现这很有用:
PHP 中文件匹配的 Glob 模式
You can use simple regular-expression-like constructs:
Given these directories:
The above glob returns:
You can narrow it down further if you like:
Given these directories:
The above glob returns:
You might find this useful:
Glob Patterns for File Matching in PHP
我建议检查 php 中的目录交互器而不是 glob
http://php.net /manual/en/class.directoryiterator.php
正如@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
As @Mike says, you can use the Regex Interator too. http://php.net/regexiterator
如果要检索与 3.0.4、3.0.5 或 3.0.10 匹配的所有目录,
您可以使用以下代码。
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.
或者通过使用正则表达式过滤器迭代器,如果您需要真正的正则表达式功能(因为 GLOB 没有),
我想它可以做得更短一些,并且可以得到增强。不管怎样,很高兴今天学到了一种新的目录过滤方法;)
Or by using a Regex Filter iterator, in case you need real regex power ('cos GLOB hasn't)
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 ;)