简体   繁体   中英

negative pattern matching to exclude certain files using glob

I am using glob to get all images from a directory. However, some of the images in the directory are thumbnails which are identified by a "-m" immediately before the file extension and I want to exclude these from the file list. So, for example there could be:

image-1.png
image-1-m.png 
portrait.png 
portrait-m.png 
front-panel-cold.png
front-panel-cold-m.png 
front-panel-warm.png 
front-panel-warm-m.png

Using

foreach(glob($imdir."/*[!m].*") as $img) {
    echo $img . "<br>\n";
}

I can list all the files which don't end with m, however that also excludes front-panel-warm.png. I can't find a way of escaping the hyphen:

glob($imdir."/*[!\-m].*")

gives no results,

glob($imdir."/*[!-m].*")

gives the same results as [!m] and

glob($imdir."/*[!--m].*")

excludes all files ending with any letter before n. I've also tried using a brace

glob($imdir."/*[!{-m}].*", GLOB_BRACE)

but that doesn't seem to work either.

Any ideas where I am going wrong, or is this simply something I need to use preg_match for?

Use this to exclude all files ending with -m

foreach(glob($imdir."/*[!-]?[!m]?.png") as $img) {
    echo $img . "<br>\n";
}

Here you go, As per the directory content you provided, I have created Two Type of Regex using glob() function, In glob function i have search alot for using two type of condition but didnt got any usefull information, so i have write it in my own way. Also Read about glob in stackoverflow answer : Link

First : Create an empty array then Loop through the directory using regex /*[!-m].* which doesnot match any files which has -m in the filename and put it in created array which $arr .

Second : Again Loop through the directory in first foreach and check for if not in_array and add it.

image-1.png
image-1-m.png 
portrait.png 
portrait-m.png 
front-panel-cold.png
front-panel-cold-m.png 
front-panel-warm.png 
front-panel-warm-m.png

$imdir = 'img';

$arr = [];
foreach(glob($imdir."/*[!-m].*") as $img) {
    $arr[] = $img;
    foreach(glob($imdir."/*[!-]?.*") as $img1) {
        if(!in_array($img1,$arr)) {
            $arr[] = $img1;
        }
    }
}

echo '<pre>';print_r($arr);echo '</pre>';

Output

Array
(
    [0] => img/front-panel-cold.jpg
    [1] => img/front-panel-warm.jpg
    [2] => img/portrait.jpg
    [3] => img/image-1.jpg
    [4] => img/portrait.jpg
)

$r = array_unique($arr);

echo '<pre>';print_r($r);echo '</pre>';

Resultant output as you required:

Array
(
    [0] => img/front-panel-cold.jpg
    [1] => img/front-panel-warm.jpg
    [2] => img/portrait.jpg
    [3] => img/image-1.jpg
)


echo " ---------------- ";
echo "</br>";


foreach(glob($imdir."/??*[!-m].*") as $img) {
    echo $img . "<br>\n";
}

**Output:**
img/front-panel-cold.jpg
img/image-1.jpg
img/portrait.jpg

echo " ---------------- ";
echo "</br>";

foreach(glob($imdir."/*[!-]?.*") as $img) {
    echo $img . "<br>\n";
}

**Output:**
img/front-panel-cold.jpg
img/front-panel-warm.jpg
img/portrait.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM