Skip to content

Instantly share code, notes, and snippets.

@funkjedi
Created January 21, 2017 22:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funkjedi/3feee27d873ae2297b8e2370a7082aad to your computer and use it in GitHub Desktop.
Save funkjedi/3feee27d873ae2297b8e2370a7082aad to your computer and use it in GitHub Desktop.
Extended glob() to support double star ** (globstar) wildcard.
<?php
function globstar($pattern, $flags = 0) {
if (stripos($pattern, '**') === false) {
$files = glob($pattern, $flags);
}
else {
$position = stripos($pattern, '**');
$rootPattern = substr($pattern, 0, $position - 1);
$restPattern = substr($pattern, $position + 2);
$patterns = array($rootPattern.$restPattern);
$rootPattern .= '/*';
while($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
$rootPattern .= '/*';
foreach($dirs as $dir) {
$patterns[] = $dir . $restPattern;
}
}
$files = array();
foreach($patterns as $pat) {
$files = array_merge($files, globstar($pat, $flags));
}
}
$files = array_unique($files);
sort($files);
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment