note 38531 added to function.glob
| From: | charl_le at alcor dot concordia dot ca | Date: | Fri, 26 Dec 2003 05:03:22 +0000 |
| Subject: | note 38531 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62467@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38531
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38531&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38531&report=yes
Search -- http://master.php.net/manage/user-notes.php