简体   繁体   English

从名称数组中的搜索中排除某些文件

[英]excluding certain files from a search within an array of names

This function grabs all the jpegs from folders which match the page name and prints a background image. 此功能从与页面名称匹配的文件夹中获取所有jpeg,并打印背景图像。 On the homepage it searches all the sub directories and chooses one at random. 在主页上,它搜索所有子目录并随机选择一个。 What i would like is to exclude (on the homepage only) certain files which match an array of names...can anyone help? 我想排除(仅在首页上)匹配名称数组的某些文件...任何人都可以帮忙吗?

$isHome = $this->level() == 0;

$path = 'public/images/bg/';
if (!$isHome) $path .= $this->slug;

$homepagefile = URL_PUBLIC.'public/images/bg/'.$this->slug.'/main.jpg';

$bgimagearray = array();
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile() && !preg_match('\.jpg$/', $fileinfo->getFilename()) &&  !$isHome) {
        $bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
    } else if ($fileinfo->isDir() && $isHome) {
        $iterator2 = new DirectoryIterator($path . $fileinfo->getFilename());
        foreach ($iterator2 as $fileinfo2) {
            if ($fileinfo2->isFile() && !preg_match('\.jpg$/', $fileinfo2->getFilename())) {
                $bgimagearray[] = "'" . $fileinfo->getFilename() . '/' . $fileinfo2->getFilename() . "'";
            }
        }
    }
}

$bgimage = array_rand($bgimagearray);

In simple terms, you just need another condition in the if statement when filtering which items go into the bgimagearray for the homepage. 简而言之,在过滤哪些项目进入首页的bgimagearray时,仅需要在if语句中使用另一个条件。

if ($fileinfo2->isFile() 
    && !preg_match('\.jpg$/', $fileinfo2->getFilename())
    && ! in_array($fileinfo2->getFilename, $my_blacklist_array) // added
) {

Aside: your code is a little messy and your task could likely be solved with prettier code structure; 另外:您的代码有点混乱,您的任务很可能可以通过更漂亮的代码结构来解决; however, that would be a rewrite of your existing code rather than answering the question posed. 但是,这将是对现有代码的重写,而不是回答所提出的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM