Try using: glob("thumbname-[0-9]*-[0-9]*.jpg")

I made a test and it works for me.

  • 10
Reply Report
      • 1
    • This seems to be the best that glob can achieve, but in the end it was too ambiguous as it would also match thumbname-2-1-1.jpg. I ended up using the RegexIterator with the regex I posted in the comments
      • 1
    • This is wrong. * matches everything, not just any of the previous expression. [0-9]* matches a number and then anything else. * is equivalent to .* in full regex, not *

Alternative to glob(), using recursiveDirectoryIterator with a regexp

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator,
  • 4
Reply Report
      • 1
    • Thanks, I may go with this method actually as the regex will give me much more control over the pattern. I've accepted the glob answer though as it was the original question.
      • 2
    • I did go with this method in the end as the glob pattern was too ambiguous. I was finding it would match thumbname-2-1-1.jpg which removed additional files. The regex I ended up using was: '/^thumbname-[0-9]+-[0-9]+(@2x)?.(jpg|jpeg|gif|png)$/i' as this gave me true arbritary digits and optional retina suffix
      • 1
    • Just noticed that it seems to fail with the start of line anchor ^ seems like the full file path gets tested by the RegexIterator not just the filename?
      • 1
    • That should be easy enoght to change, simply by removing the start anchor... the end anchor remains, and aqs the first part of the regex is a fixed "thumbnail" it should still work correctly

You could split the numbers into different glob arrays and merge them.

For example first you take all images from 0-9 into one array:

$g1 =  glob('thumbname-[0-9].jpg');

After that you create another array which holds the numbers from 10 - 99:

$g2 =  glob('thumbname-[0-9][0-9].jpg');

Now you merge those 2 arrays into one and get all from 0-99:

$gResult = array_merge($g1, $g2);

You can extent it as much as you want just add a [0-9]

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions