note 50600 added to function.scandir
| From: | csaba at alum dot mit dot edu | Date: | Fri, 04 Mar 2005 12:52:55 +0000 |
| Subject: | note 50600 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85916@lists.php.net to get a copy of this message | ||
A nice way to filter the files/directories you get back from scandir:
<?php
function pathFilter ($path, $aFilter) {
// returns true iff $path survives the tests from $aFilter
// $aFilter is an array of regular expressions: [-]/regExp/modifiers
// if there is a leading '-' it means exclude $path upon a match (a NOT test)
// If the first expression has a leading '-', $path is in, unless it gets excluded.
// Otherwise, $path is out, unless it gets included via the filter tests.
// The tests are applied in succession.
// A NOT test is applied only if $path is currently (within the tests) included
// Other tests are applied only if $path is currently excluded. Thus,
// array("/a/", "-/b/", "/c/") => passes if $path has a c or if
$path has an a but no b
// array("/a/", "/c/", "-/b/") => passes if $path has an a or c,
but no b
// array("-/b/", "/a/", "/c/") => passes if $path has no b, or
if $path has an a or c
if (!$aFilter) return true; // automatic inclusion (pass) if no filters
foreach ($aFilter as $filter) break; // we don't know how it's indexed
$in = $filter[0]=="-"; // initial in/exclusion based on first filter
foreach ($aFilter as $filter) // walk the filters
if ($in==$not=($filter[0]=="-")) // testing only when necessary
$in ^= preg_match(substr($filter,$not),$path); // flip in/exclusion upon a match
return $in;
}
?>
Csaba Gabor from Vienna
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50600
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50600&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50600&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50600&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50600&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50600&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50600&report=yes
Search -- http://master.php.net/manage/user-notes.php