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 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面