Re: filter files in a directory
| From: | Justin French | Date: | Tue, 27 Aug 2002 02:15:08 +0000 |
| Subject: | Re: filter files in a directory | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-114210@lists.php.net to get a copy of this message | ||
You need to have a look at the regular expression functions like ereg,
eregi, ereg_replace, eregi_replace and the preg functions.
The manual itself doesn't document regular expression pattern matching that
well, but there is a reasonable starter article here:
http://www.phpbuilder.com/columns/dario19990616.php3
Anyhoo, rather than testing for
$files != "*.config"
you need to test for
!ereg('.config$', $files)
which means if the file doesn't END with .config.
Similarly, you could also rule out .. and . with another regular expression:
!eregi('^.', $files)
Revised code, untested:
<?
if($handle = opendir('/path/to/list/directory'))
{
while(false !== ($files = readdir($handle)))
{
if(!eregi('^.', $files) && !eregi('.config$', $files))
{
echo $files;
}
}
}
?>
Justin French
on 27/08/02 4:03 AM, rdkurth@starband.net (rdkurth@starband.net) wrote:
> I am showing a directory of files and I don't what all the files to
> show on the screen. I was able to get the . and the .. not to show
> with what I have below but there are some other files in there that look
> like this mylist.confg. I don't want them to show in the directory
> list. I tried && $files != "*.config" but this did not work .
> Anybody have an suggestions on how to do this . I have look through
> the manual and the mailing list but can't figure out how to make the
> pattern to keep this from showing
>
> if ($handle = opendir('/path/to/list/directory')) {
> while (false !== ($files = readdir($handle))) {
> if ($files != "." && $files != "..") {
> echo $files;
> }
>