note 38531 deleted from function.glob by aidan
| From: | aidan@php.net | Date: | Mon, 22 Nov 2004 12:04:37 +0000 |
| Subject: | note 38531 deleted from function.glob by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80858@lists.php.net to get a copy of this message | ||
Note Submitter: charl_le at alcor dot concordia dot ca
----
Just a small modification of Jirka's glob function; I was running into problems with foreach.
E.g.
foreach( glob( "/path/*.doc" ) as $entry ) { [something] }
Assuming the folder contained no files with a .doc extension glob appears to return a null value (?)
and I would get an error message stating that an invalid argument was passed to the foreach loop.
The following modification appears to work pretty good with PHP 4.2.2
function glob ($pattern) {
$path_parts = pathinfo ($pattern);
$pattern = '^' . str_replace (array ('*', '?'), array
('(.+)', '(.)'), $path_parts['basename'] . '$');
$dir = opendir ($path_parts['dirname']);
while ($file = readdir ($dir)) {
if (ereg ($pattern, $file)) $result[] = "{$path_parts['dirname']}/$file";
}
closedir ($dir);
// my changes here
if ( isset($result) )
return $result;
return (array) null;
}
I hope this might help someone and any suggections are welcome. ~CL