doutui6241 2016-05-07 12:02
浏览 45
已采纳

将glob()函数与数组和通配符搜索一起使用

I currently have an array which holds file names, I now want to foreach through this array using a wildcard search in a directory and find the matching files.

How can I do this using the glob() function?

  • 写回答

1条回答 默认 最新

  • dora1989 2016-05-07 12:16
    关注

    You can simply do this to extract each file which matches your wildcard search inside a foreach loop:

    $search = "*.txt";
    // if this is what you mean by indexed string variable?
    $file = array("updated.txt", "new.txt"); 
    
    // getcwd() will retrieve the current directory but is not needed - this just shows
    // that you can supply a directory if you wanted to.
    
    function MatchFile($file,$search)
    {
        $temp = array();
        $i = 0;
        foreach (glob(getcwd().$search) as $filename) :
            if (count($file) > $i):
                array_push($temp, ($filename == $file[$i++])? $filename : "");
            else:
                // stops the index overflow error
                break;
            endif;
        endforeach;
        return $temp;
    }
    
    foreach (MatchFile($file,$search) as $found):
        echo $found . " was found!";
    endforeach;
    

    This is just a replacement of opendir();.

    If you're still unsure or need to know more flags, you can use this link for help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势