如何在PHP中使用scandir获取过滤的文件名列表?

我想在文件夹中获取所有’_img’和PDF类型的文件

而不是使用

$fileArray = scandir($dir);
foreach ($fileArray as $file) {
    if (preg_match("_img",$file) && pathinfo($file,PATHINFO_EXTENSION) == 'pdf'){
        $filteredArray[] = $file;
    }
}

有没有捷径或者这是最好的方式?谢谢

解决方法

使用PHP glob()功能

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function,which is similar to the rules used by common shells

<?PHP 
  $filteredArray = array();
  foreach (glob($dir."/*_img.pdf") as $filename)
     $filteredArray[] = $filename;
?>

还有fnmatch()功能,它将文件名与模式匹配

最后一个解决方案是使用DirectoryIteratorFilterIterator

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...