SPL的遞歸迭代器是這種類型的功能特別有用:
abstract class FilesystemRegexFilter extends RecursiveRegexIterator {
protected $regex;
public function __construct(RecursiveIterator $it, $regex) {
$this->regex = $regex;
parent::__construct($it, $regex);
}
}
class FilenameFilter extends FilesystemRegexFilter {
// Filter files against the regex
public function accept() {
return (! $this->isFile() || preg_match($this->regex, $this->getFilename()));
}
}
class DirnameFilter extends FilesystemRegexFilter {
// Filter directories against the regex
public function accept() {
return (! $this->isDir() || preg_match($this->regex, $this->getFilename()));
}
}
$directory = new RecursiveDirectoryIterator(realpath(__DIR__ . '../fonts'));
$filter = new DirnameFilter($directory, '/^(?!\.)/');
$filter = new FilenameFilter($filter, '/(?:ttf)$/i');
$myArray = [];
foreach(new RecursiveIteratorIterator($filter) as $file) {
$myArray[] = $file;
}
雖然不知道爲什麼你需要建立一個數組,而不要簡單地與文件工作中的foreach
循環內
尋找glob()http://php.net/manual/en/function.glob.php – Satya
這看起來更像是**規範,而不是與特定問題有關的問題**。對不起,我們不在這裏爲你免費爲你工作。盡一切努力來解決問題,然後就特定問題尋求幫助。 – RiggsFolly
[如何從使用PHP的文件夾中讀取文件的列表](http://stackoverflow.com/questions/720751/how-to-read-a-list-of-files-from-a-文件夾使用PHP) – thomasb