Here's all-in-one DirectoryIterator:
<?php
class RRDI extends RecursiveIteratorIterator {
public function __construct($path, $flags = 0) {
parent::__construct(new RecursiveDirectoryIterator($path, $flags));
}
}
class AdvancedDirectoryIterator extends FilterIterator {
private $regex;
public function __construct($path, $flags = 0) {
if (strpos($path, '-R ') === 0) { $recursive = true; $path = substr($path, 3); }
if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) { $path = substr($path, 0, -strlen($matches[1]) - 1); $this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; if (!$path) $path = '.'; }
parent::__construct($recursive ? new RRDI($path, $flags) : new DirectoryIterator($path));
}
public function accept() { return $this->regex === null ? true : preg_match($this->regex, $this->getInnerIterator()->getFilename());
}
}
?>
Some examples:
<?php
foreach (new AdvancedDirectoryIterator('.') as $i) echo $i->getPathname() . '<br/>';
foreach (new AdvancedDirectoryIterator('-R *.php') as $i) echo $i->getPathname() . '<br/>';
foreach (new AdvancedDirectoryIterator('-R js/jquery-*.js') as $i) echo $i->getPathname() . '<br/>';
?>
Pretty cool, huh? :)