Glob pattern matching the first part of a file name

谁都会走 提交于 2019-12-12 13:36:25

问题


In a directory I have filenames like 123X1.jpg, 23X1.jpg, 23X2.jpg, 4123X1.jpg. I need the glob pattern to only get listed files starting with a required string.

For example:

'23X' -> 23X1.jpg, 23X2.jpg
'123X' -> 123X1.jpg

Last part part of the pattern is always an X. The first one is a number.


回答1:


It's trivial with glob():

print_r(glob('/path/to/23X*.jpg'));
print_r(glob('/path/to/123X*.jpg'));



回答2:


You can try RegexIterator

$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
$regex = new RegexIterator($fi, "/\dX[a-z\d]+/i");

foreach($regex as $file) {
    echo (string) $file, PHP_EOL;
}


来源:https://stackoverflow.com/questions/16492775/glob-pattern-matching-the-first-part-of-a-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!