简体   繁体   中英

Glob patterns for file matching in PHP - Get directory contents which do not start with a string group

How can I ignore all images that begin with the string 'blogCover'?

At first I did not exclude a picture for the test

all pictures in img directory:

$imgDir = 'img';
$imgs = glob($imgDir.'/*{.jpg}', GLOB_BRACE); //all images!
echo'<pre>'; print_r($imgs); echo'</pre>';

result:

Array
(
    [0] => img/blogCover-large.jpg
    [1] => img/blogCover-large_2x.jpg
    [2] => img/blogCover-medium.jpg
    [3] => img/blogCover-medium_2x.jpg
    [4] => img/blogCover-small.jpg
    [5] => img/blogCover-small_2x.jpg
    [6] => img/boddy-large.jpg
    [7] => img/boddy-large_2x.jpg
    [8] => img/boddy-medium.jpg
    [9] => img/boddy-medium_2x.jpg
    [10] => img/boddy-small.jpg
    [11] => img/boddy-small_2x.jpg
    [12] => img/face-large.jpg
    [13] => img/face-large_2x.jpg
    [14] => img/face-medium.jpg
    [15] => img/face-medium_2x.jpg
    [16] => img/face-small.jpg
    [17] => img/face-small_2x.jpg
    [18] => img/hand-large.jpg
    [19] => img/hand-large_2x.jpg
    [20] => img/hand-medium.jpg
    [21] => img/hand-medium_2x.jpg
    [22] => img/hand-small.jpg
    [23] => img/hand-small_2x.jpg
    [24] => img/head-large.jpg
    [25] => img/head-large_2x.jpg
    [26] => img/head-medium.jpg
    [27] => img/head-medium_2x.jpg
    [28] => img/head-small.jpg
    [29] => img/head-small_2x.jpg
)

my goal: Ignore all images that begin with the string 'blogCover'

$imgDir = 'img';
$imgs = glob($imgDir.'/*{[!blogCover*].jpg}', GLOB_BRACE);
echo'<pre>'; print_r($imgs); echo'</pre>';

unexpected result:

Array
(
    [0] => img/blogCover-large_2x.jpg
    [1] => img/blogCover-medium.jpg
    [2] => img/blogCover-medium_2x.jpg
    [3] => img/blogCover-small_2x.jpg
    [4] => img/boddy-large_2x.jpg
    [5] => img/boddy-medium.jpg
    [6] => img/boddy-medium_2x.jpg
    [7] => img/boddy-small_2x.jpg
    [8] => img/face-large_2x.jpg
    [9] => img/face-medium.jpg
    [10] => img/face-medium_2x.jpg
    [11] => img/face-small_2x.jpg
    [12] => img/hand-large_2x.jpg
    [13] => img/hand-medium.jpg
    [14] => img/hand-medium_2x.jpg
    [15] => img/hand-small_2x.jpg
    [16] => img/head-large_2x.jpg
    [17] => img/head-medium.jpg
    [18] => img/head-medium_2x.jpg
    [19] => img/head-small_2x.jpg
)

all '* -large.jpg', '* -small.jpg' images are missing

expected result:

Array
(
    [0] => img/boddy-large.jpg
    [1] => img/boddy-large_2x.jpg
    [2] => img/boddy-medium.jpg
    [3] => img/boddy-medium_2x.jpg
    [4] => img/boddy-small.jpg
    [5] => img/boddy-small_2x.jpg
    [6] => img/face-large.jpg
    [7] => img/face-large_2x.jpg
    [8] => img/face-medium.jpg
    [9] => img/face-medium_2x.jpg
    [10] => img/face-small.jpg
    [11] => img/face-small_2x.jpg
    [12] => img/hand-large.jpg
    [13] => img/hand-large_2x.jpg
    [14] => img/hand-medium.jpg
    [15] => img/hand-medium_2x.jpg
    [16] => img/hand-small.jpg
    [17] => img/hand-small_2x.jpg
    [18] => img/head-large.jpg
    [19] => img/head-large_2x.jpg
    [20] => img/head-medium.jpg
    [21] => img/head-medium_2x.jpg
    [22] => img/head-small.jpg
    [23] => img/head-small_2x.jpg
)

I expect to ignore these pictures:

  • blogCover-large.jpg
  • blogCover-large_2x.jpg
  • blogCover-medium.jpg
  • blogCover-medium_2x.jpg
  • blogCover-small.jpg
  • blogCover-small_2x.jpg

The right syntax is:

$imgs = glob($imgDir.'/[^blogCover]*.jpg', GLOB_BRACE);

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