Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adam-lynch/5397461 to your computer and use it in GitHub Desktop.
Save adam-lynch/5397461 to your computer and use it in GitHub Desktop.
Iterators on iterators. Get all filenames in a directory (recursively) which matches a given regular expression. Iterators: RegexIterator, RegexIteratorIterator, RegexDirectoryIterator, RecursiveRegexIterator.
$iterator = new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::LEAVES_ONLY
),
'%^.*?(([^\\\]+?)\.png)$%',
\RecursiveRegexIterator::GET_MATCH
);
foreach ($iterator as $file) {
// CAN access groups / references in regular expression used in RegexIterator :)
print_r($file);
/*
* Example output:
* array ( [0] => C:\directory\filename.png [1] => filename.png [2] => filename )
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment