通过特定目录树迭代,并将所有.ttf文件存储在PHP中的数组中 [英] Iterate through a specific directory tree and store all the .ttf files into an array in PHP

查看:208
本文介绍了通过特定目录树迭代,并将所有.ttf文件存储在PHP中的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有很多文件,其中一些是在这些路径中:

  root / fonts / folder1 / font1.ttf 
root / fonts / folder1 / font2.ttf
root / fonts / folder2 / font1.ttf
root / fonts / folder2 / font2.ttf
root / scripts / file.php

请记住,这些文件夹中还有其他类型的文件。我的/ scripts / file.php可以通过../fonts/目录树遍历并存储所有的TrueType字体(.ttf) 文件转换成数组?你能给我一个例子吗?

解决方案

SPL的递归迭代器对这种类型的功能特别有用:

 抽象类FilesystemRegexFilter extends RecursiveRegexIterator {
protected $ regex;
public function __construct(RecursiveIterator $ it,$ regex){
$ this-> regex = $ regex;
parent :: __ construct($ it,$ regex);
}
}

class FilenameFilter extends FilesystemRegexFilter {
//根据正则表达式过滤文件
public function accept(){
return !$ this-> isFile()|| preg_match($ this-> regex,$ this-> getFilename()));
}


class DirnameFilter extends FilesystemRegexFilter {
//根据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中的文件循环


Lets say I have a lot of files, some of those are in these paths:

root/fonts/folder1/font1.ttf
root/fonts/folder1/font2.ttf
root/fonts/folder2/font1.ttf
root/fonts/folder2/font2.ttf
root/scripts/file.php

Remember that there are also other types of files in those folders. How can my "/scripts/file.php" iterate through the "../fonts/" directory tree and store all the TrueType font (.ttf) files into an array? Could you show me an example?

解决方案

SPL's recursive iterators are particularly useful for this type of functionality:

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;
}

Though not sure why you need to build an array, and don't simply work with the files inside your foreach loop

这篇关于通过特定目录树迭代,并将所有.ttf文件存储在PHP中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆