【问题标题】:preg_match to match files with .jpg/.png extensionpreg_match 匹配扩展名为 .jpg/.png 的文件
【发布时间】:2013-08-27 07:35:35
【问题描述】:

我有一个根文件夹C:/wamp/www//scanner/scanner/。 例如:此文件夹包含 directories imagesworkuploads 等。我正在使用函数 scan_dir 扫描所有文件夹和子文件夹等。 我想排除带有这些扩展名的文件。

$except = array("rar", "zip", "mp3", "mp4", "mp3", "mov", "flv", "wmv", "swf", "png", "gif", "jpg", "bmp", "avi");

谁能帮帮我。

【问题讨论】:

  • 到目前为止你尝试过什么?因为我假设您使用循环来遍历文件。为什么不忽略那里的文件类型?
  • 而不是排除,如何将数组用于要包含的文件,我想这将是一个较小的列表

标签: php regex arrays


【解决方案1】:

类似下面的东西?

$except = array("rar", "zip", "mp3", "mp4", "mp3", "mov", "flv", "wmv", "swf", "png", "gif", "jpg", "bmp", "avi");

if (!preg_match('/\.('.implode('|', $except).')$/', $filename, $matches)) {
  // ...
}

【讨论】:

    【解决方案2】:

    试试这个:

    $except = array("rar", "zip", "mp3", "mp4", "mp3", "mov", "flv", "wmv", "swf", "png", "gif", "jpg", "bmp", "avi");
    $imp = implode('|', $except);
    
    foreach($files as $file)
    {
        if(preg_match('/^.*\.('.$imp.')$/i', $file))
            echo $file;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多