Help with glob template

It would be nice if someone could give me a regex pattern for glob to get below file names:

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

returns files without "_thumb". I have this template:

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

and that will give me all the "_thumb".

+5
source share
4 answers

glob()It’s not the biggest one when handling situations where you have complex file mapping requirements, as you have clearly noticed. I would recommend using the PHP SPL library and using 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();
    }
}

( , - ).

+6

. PHP glob , . , [0-9]*.jpg, .

+3
foreach (glob('[0-9]*') as $filename) {
    if (strpos("$filename","_thumb") === FALSE){
        echo "$filename \n";
    }
}
+3

zombat use DirectoryIterator , (. foreach ) .

class DirectoryFilterThumbs extends FilterIterator {
    public function __construct($path) {
        parent::__construct(new DirectoryIterator($path));
    }
    public function accept() {
        // Use regex or whatever you like here
        return ($this->isFile() && strpos($this->getFilename(), "_thumb.") === FALSE);
    }
}

$files = new DirectoryFilterThumbs("/dir/path");
foreach ($files as $file) {
    echo $file->getFilename() . PHP_EOL;
}

, , /, .

+1
source

All Articles