RegexIterator以与文件路径匹配

时间:2019-03-13 16:04:27

标签: php regex recursion

我正在编写一个函数来递归遍历目录并返回其中的文件。目录将呼叫记录存储在遵循命名约定'YY/mm/dd'的文件路径中。

示例文件路径如下所示:/vhosts/example.org/private/recordings/2018/11/21/user@example.org_XXXXXXXXX

我的脚本应该遍历给定时间段内所有可能的文件路径,然后遍历结果以过滤出与某个人匹配的文件路径。由于文件是以这种方式存储的,因此我不是在使用filemtime(),而是在构建正则表达式来匹配记录的文件路径。

//make the date into a regular expression blocks
//this is only a small abstract of the logic that builds the regular expression
$year = date_format($start_date, 'Y');
$months = '([' . date_format($start_date, 'm') . '-' . date_format($end_date, 'm') . '])';
$days = '([' . date_format($start_date, 'd') . '-' . date_format($end_date, 'd') . '])';


//lets assume this was the regex built by that logic
$regex = preg_quote("2018/11/21");


//traverse and build the iterator
$directory = new RecursiveDirectoryIterator($root_directory);
$results = new RecursiveIteratorIterator($directory);

if($condition){
   $results = new RegexIterator($results, "#$regex*#", RecursiveRegexIterator::GET_MATCH);
}

我的问题是RegexIterator是否与文件名或文件路径匹配,还是两者都匹配?,而无需输入if语句,我得到此响应。当我输入if语句时,我得到一个空对象。我尚未针对特定用户实施检查,因此我首先确保可以相应地缩短时间段。

object(SplFileInfo)#9 (2) {
  ["pathName":"SplFileInfo":private]=>
  string(112) "/vhosts/example.org/private/recordings/2018/11/21/user@example.org_82342342.wav"
  ["fileName":"SplFileInfo":private]=>
  string(61) "user@example.org_82342342.wav"
}

0 个答案:

没有答案