1

我正在尝试使用 glob 在我的目录中获取某些文件。

我的模式就像

foreach(glob($root . "../test/te[0-9]{2}.xml") as $filename){
  echo $filename;
}

这些文件是

0051_001.xml
0071_001_as01.xml
0485_001_te01.xml
0485_001_te02.xml
0485_001_teh03.xml

它什么也不输出。

我只想要teh01te03.xml不是其余的。我不确定我的模式有什么问题。有人可以帮我吗?非常感谢!

4

1 回答 1

1

glob不支持[0-9]{2},你必须写[0-9][0-9]

foreach(glob($root . "../test/te[0-9][0-9].xml") as $filename){
  echo $filename;
}

对于您的文件,您需要使用:

foreach(glob($root . "../test/[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9]_te[0-9][0-9].xml") as $filename){
  echo $filename;
}

或者您可以添加正则表达式检查:

foreach(glob($root . "../test/*.xml") as $filename){
  if (preg_match('/_te\d{2}\.xml$/', $filename, $matches)){
     echo $filename;
  }
}
于 2013-03-21T00:42:13.127 回答