Match multiple file extensions with fnmatch ()

In a typical file matching code:

while (false !== ($file = readdir($handle))) {
    if ($file !== "." && $file !== ".." && fnmatch($mask, $file)) {
        $dirList[] = $file;
    }
}

I tried to use the multiple file extension template as follows:

$mask = "*.{jpg,png,gif}";

but that will not work. No files. This is rather strange since the same template works fine in the terminal. It also works great with glob (), returning the files I want. I know that there is nothing wrong with the rest of the code, because using "* .jpg" as a mask also works with fnmatch. Was fnmatch supposed to not support the same patterns that are available in the shell?

+3
source share
2 answers

Not supported.

AFAIK *.{ext,alt} - ksh bash . fnmatch() - , Unix- ( BSD , Linux/glibc, , ). FNM_EXTMATCH, , , {alt}. PHP / .

glob() GLOB_BRACE, .{jpeg,png,gif}. readdir().

+4

- , bash, fnmatch() ( ). Windows ( ) php.net, , regex.

0

Source: https://habr.com/ru/post/1780491/


All Articles