glob
is very much what you want here. However, being as you appear to be using Laravel (due to the tag), you should look at Laravel's FileSystem
class (accessible using the File
facade): 4.2 docs. It provides a wrapper for the standard PHP file functions (though doesn't actually add anything to the mix really for your purposes).
You can do stuff like this:
if (File::isDirectory($dir)) {
foreach (File::glob($dir.'/project-1-*') as $projectDir) {
$actualDir = substr($projectDir, strlen($dir) + 1); // trim the base directory
}
}
But if you want a more powerful file-finding system, you can use Symfony's Finder
component (docs), which is already included in your project if you're using Laravel (as some Laravel Filesystem
methods use it) and you'll be able to do things like this:
$dirs = Finder::create()->in($dir)->depth(0)->directories()->name('project-1-*');
// or, if you want to use regex, this should work
$dirs = Finder::create()->in($dir)->depth(0)->directories()->name('/^project-1-/');
Now, Finder returns an iterator, so you can use PHP's iterator_to_array
function to turn it into an array if you need to use it as an array (that said, an iterator is better if you don't need it as an array, for instance foreach
ing over it).