tags:

views:

57

answers:

5
print_r(glob('*',GLOB_ONLYDIR));

gives me all directories I need only the ones that starts with a digit (version numbers 3.0.4, 3.0.5 etc). I guess I can use for each and some condition... wondering if there is any other 'nicer' way to do that.

A: 
foreach(glob('*',GLOB_ONLYDIR) as $directoryname)
{
    if (strstr('0123456789',substr($directoryname,0,1))!="")
    {
         //$directoryname starts with a digit
    }
}
Volker
+10  A: 

You can use simple regular-expression-like constructs:

print_r(glob("[0-9]*", GLOB_ONLYDIR));

Given these directories:

12test
1test
2test
test

The above glob returns:

Array
(
    [0] => 12test
    [1] => 1test
    [2] => 2test
)

You can narrow it down further if you like:

print_r(glob("[0-9]\.[0-9]\.[0-9]*", GLOB_ONLYDIR));

Given these directories:

3.0.4.Test
3.0.Test

The above glob returns:

Array
(
    [0] => 3.0.4.Test
)

You might find this useful:
Glob Patterns for File Matching in PHP

Mike
I knew it would be simple. Thank you.
Radek
+3  A: 

I would recommend checking out the directory interator in php rather than glob

http://php.net/manual/en/class.directoryiterator.php

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        // check if $fileinfo->getFilename() matches your criteria
    }
}
?>

As @Mike says, you can use the Regex Interator too. http://uk2.php.net/regexiterator

Lizard
OOP, Would be a better solution than glob
RobertPitt
You can filter the [DirectoryIterator](http://php.net/manual/en/class.directoryiterator.php) with the [RegexIterator](http://uk2.php.net/regexiterator) too.
Mike
Since PHP5.3 you an also use the [GlobIterator](http://de.php.net/manual/en/class.globiterator.php)
Gordon
@RobertPit I disagree. It depends entirely on what he wants to do. If he just wants an array of the directory names, `glob()` is just fine. No need to instantiate an iterator and add elements to an array yourself when there is a built-in function that does it for you.
Daniel Egeberg
Dont get me wrong, Glob is powerful and a sexy little function, its stable and fast, My point was that over time and has projects grow an OOP Style would be my choice, but if its a 1 time only Glob would be good.(all depends what the application is based around).
RobertPitt
A: 

If you want to retrieve all directories that matches 3.0.4, or 3.0.5 or 3.0.10 , you can use the following code.

$dirs = glob("[0-9]+\.[0-9]+\.[0-9]+", GLOB_ONLYDIR);
syabac
A: 

Or by using a Regex Filter iterator, in case you need real regex power ('cos GLOB hasn't)

$dir_iterator = new DirectoryIterator('./');
$regex = '#^\d.*$'; // something really basic for now
$iterator = new RegexIterator($dir_iterator, $regex);

foreach ($iterator as $dir_object)
   {
   if ($dir_object->isDir())
       {
       // Just do something with it.
       echo $dir_object->getPathname()."<br/>\n"; 
       }
   }

I imagine it could be done a little bit shorter, and it could be enhanced. Anyway, glad to have learned a new approach towards directory filtering today ;)

Berzemus