©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 5 >= 5.3.0)
GlobIterator::__construct — Construct a directory using glob
$path
[, int $flags
= FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
] )Constructs a new directory iterator from a glob expression.
path
The path of the directory.
flags
Option flags, the flags may be a bitmask of the FilesystemIterator constants.
Example #1 GlobIterator example
<?php
$iterator = new GlobIterator ( '*.dll' , FilesystemIterator :: KEY_AS_FILENAME );
if (! $iterator -> count ()) {
echo 'No matches' ;
} else {
$n = 0 ;
printf ( "Matched %d item(s)\r\n" , $iterator -> count ());
foreach ( $iterator as $item ) {
printf ( "[%d] %s\r\n" , ++ $n , $iterator -> key ());
}
}
?>
以上例程的输出类似于:
Matched 2 item(s) [1] php5ts.dll [2] php_gd2.dll
[#1] alek [2015-02-09 19:26:35]
As seen in example above, first parameter in the GlobIterator constructor is not a path but a pattern which listed filenames should match.
[#2] ozana at omdesign dot cz [2014-03-29 10:08:23]
GlobIterator doesn??t have a way to support GLOB_BRACE!
<?php
$iterator = new GlobIterator(getcwd. '
$iterator
)$iterator
)[#1] vascowhite at gmail dot com [2013-06-04 13:54:09]
It is important to realise that rewind() must be called on any iterator before using it or you may experience undefined behaviour, see example code and output here http://3v4l.org/rvNpU
See this bug report https://bugs.php.net/bug.php?id=63823&edit=2 for a fuller explanation.
[#2] Anonymous [2012-10-16 00:47:36]
to loop through object keys and reset to the start, try this:
<?php
$obj = new stdClass();
$obj->Mon = "Monday";
$obj->Tue = "Tuesday";
$obj->Wed = "Wednesday";
$obj->Thu = "Thursday";
$obj->Fri = "Friday";
$obj->Sat = "Saturday";
$obj->Sun = "Sunday";
$infinate = new InfiniteIterator(new ArrayIterator($obj));
foreach ( new LimitIterator($infinate, 0, 14) as $value ) {
print($value . PHP_EOL);
}
?>
will output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Can be useful when doing date operations or recurring events