Created
January 21, 2017 22:38
-
-
Save funkjedi/3feee27d873ae2297b8e2370a7082aad to your computer and use it in GitHub Desktop.
Extended glob() to support double star ** (globstar) wildcard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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