Matching php glob pattern for number of numbers in arbitration

Is it possible to match an arbitrary number of digits with the php glob function? I am trying to match file names for thumbnails of images that end in sizes from two to four digits.

I know that I can provide a range of numbers, but this only matches one character:

glob("thumbname-[0-9]-[0-9].jpg") 

This will match thumbname-1-1.jpg, but not thumbname-10-10.jpg, etc.

+6
source share
3 answers

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

I did a test and it works for me.

+3
source

Alternative glob () using recursiveDirectoryIterator with regex

 $Directory = new RecursiveDirectoryIterator('path/to/project/'); $Iterator = new RecursiveIteratorIterator($Directory); $Regex = new RegexIterator($Iterator, '/^thumbname-[0-9]*-[0-9]*\.jpg$/i', RecursiveRegexIterator::GET_MATCH ); 
+4
source

You can split the numbers into different glob arrays and combine them.

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

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

After that, you create another array that contains numbers from 10 to 99:

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

Now you combine these 2 arrays into one and get everything from 0 to99:

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

You can expand it as much as you want, just add [0-9]

0
source

All Articles