note 35129 deleted from function.glob by aidan
| From: | aidan@php.net | Date: | Thu, 09 Dec 2004 03:41:59 +0000 |
| Subject: | note 35129 deleted from function.glob by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-81560@lists.php.net to get a copy of this message | ||
Note Submitter: nutbar at innocent dot com
----
Piping the output of "ls" and parsing that if you don't have glob() handy is kind of
a bad hack. Here's a far better implementation that uses normal PHP functions that you will
find in < 4.3.0.
if ($fp = opendir('.')) {
while (($file = readdir($fp)) !== FALSE) {
if (is_file($file) && preg_match('/\.txt$/', $file)) {
echo "$file\n";
}
}
closedir($fp);
}
It's set to return only files, not directories, but you can change that easily by removing the
is_file() check. Also, this gives you the extra power of a full regex pattern matcher which glob()
may not be able to provide.