简体   繁体   English

球形模式以同时匹配两个日期

[英]glob pattern to match two dates simultaneously

I'm using glob in this format at the moment: 我目前正在以这种格式使用glob:

glob('/STORAGE/LOGFILES/*.*')

Within /STORAGE/LOGFILES, there are hundreds of text files that all begin with YYYY-MM-DD. 在/ STORAGE / LOGFILES中,有数百个均以YYYY-MM-DD开头的文本文件。 I'd like to change the *.* in my pattern to instead only look at the current month and previous month: 我想将模式中的*.*更改为仅查看当月和上个月:

glob('/STORAGE/LOGFILES/2013-04*')
glob('/STORAGE/LOGFILES/2013-03*')

What I can't seem to figure out is how to do this in a single statement. 我似乎无法弄清楚如何在单个语句中执行此操作。 I think I've figured out how to do the current month: 我想我已经弄清楚当月的做法:

glob('/STORAGE/LOGFILES/'.date("Y-m").'*')

but I'd like a way to combine it all into a single statement. 但我想要一种将所有内容合并为一条语句的方法。 Appreciate any help and thank you! 感谢任何帮助,谢谢!

You can use GLOB_BRACE option like this: 您可以像这样使用GLOB_BRACE选项:

$now = new DateTime();
$lastMonth = new DateTime('-1 month');
$monthList = $now->format('Y-m').','.$lastMonth->format('Y-m');
$pattern = '/STORAGE/LOGFILES/{'.$monthList.'}*';
glob($pattern, GLOB_BRACE);

try this code 试试这个代码

glob('/STORAGE/LOGFILES/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*')

You can play with glob pattern : http://cowburn.info/2010/04/30/glob-patterns/ Have fun 您可以使用glob模式: http : //cowburn.info/2010/04/30/glob-patterns/玩得开心

It seems to me that is platform (or shell) depended. 在我看来,这取决于平台(或外壳)。

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. glob()函数根据libc glob()函数所使用的规则搜索所有与模式匹配的路径名,该规则类似于普通shell所使用的规则。

See this note: http://www.php.net/manual/en/function.glob.php#110340 请参阅此注释: http : //www.php.net/manual/zh/function.glob.php#110340

<?php

$a = glob('/path/*{foo,bar}.dat',GLOB_BRACE);
print_r($a);

?>

Result : 
Array
(
    [0] => /path/file_foo.dat
    [1] => /path/file_foobar.dat
    [2] => /path/file_foobar.dat
)

Maybe you could try an '|' 也许您可以尝试使用“ |” or sign as well? 还是签名?

Doesn't it support regex: http://www.php.net/manual/en/function.glob.php#88250 ? 它不支持正则表达式: http : //www.php.net/manual/zh/function.glob.php#88250吗?

怎么样

glob('/STORAGE/LOGFILES/'.date("Y").'-{'.date("m").','.date("m")-1.'}*',GLOB_BRACE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM