views:

383

answers:

4

I want all CSV files in a directory, so I use

glob('my/dir/*.CSV')

This however doesn't find files with a lowercase CSV extension.

I could use

glob('my/dir/*.{CSV,csv}', GLOB_BRACE);

But is there a way to allow all mixed case versions? Or is this just a limitation of glob() ?

+1  A: 

You could do this

$files = glob('my/dir/*');

$csvFiles =  preg_grep('/\.csv$/i', $files);
alex
A: 

If you want glob to be case-insensitive, you need to build a regex to pass in. Something like

glob("i~my/dir/*.CSV~");

Alternatively if you're using an older version of PHP you can use the sql_regcase() function.

$pattern = sql_regcase("my/dir/*.CSV");
glob($pattern);

However, this function is deprecated as of PHP 5.3 so you probably should just build the regex yourself.

David
glob() doesn't take a regex. from http://ca3.php.net/manual/en/function.glob.php : "The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells."::::::::::: `man 7 glob`: "Note that wildcard patterns are not regular expressions."
intuited
+5  A: 

Glob patterns support character ranges:

glob('my/dir/*.[cC][sS][vV]')
Ignacio Vazquez-Abrams
hmm.. guess I should refresh more often. cheers.
intuited
+1  A: 

glob('my/dir/*.[cC][sS][vV]') should do it. Yeah it's kind of ugly.

intuited