Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - help with glob pattern

It would be nice if someone could give me a regexp pattern for glob for getting below filenames:

1.jpg // this file
1_thumb.jpg
2.png // this file
2_thumb.png
etc...

returning the files without "_thumb". I have this pattern:

$numericalFiles = glob("$this->path/*_thumb.*");

and that give me all with "_thumb."

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

glob() isn't the greatest at handling situations where you have complex requirements for file matching, as you've clearly noticed. I'd recommend using PHP's SPL library and taking advantage of the DirectoryIterator class.

$iterator = new DirectoryIterator("/dir/path");
foreach ($iterator as $file) {
    if ($file->isFile() && preg_match("/^[0-9]+./i",$file->getFilename())) {
        echo $file->getFilename();
    }
}

You can modify your criteria cleanly during the iteration (also, it's easy to modify the iterator if you ever needed recursive directory iteration).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...