Is it possible to match an arbitrary number of digits with the php glob function? I'm trying to match file names for image thumbnails that end with dimensions ranging from two to four digits.
I know I can supply a digit range, but this only matches a single character:
glob("thumbname-[0-9]-[0-9].jpg")
This will match thumbname-1-1.jpg but not thumbname-10-10.jpg, etc.
The glob() function returns an array of filenames or directories matching a specified pattern.
Regex and Glob patterns are similar ways of matching patterns in strings. The main difference is that the regex pattern matches strings in code, while globbing matches file names or file content in the terminal. Globbing is the shell's way of providing regular expression patterns like other programming languages.
Try using: glob("thumbname-[0-9]*-[0-9]*.jpg")
I made a test and it works for me.
Alternative to glob(), using recursiveDirectoryIterator with a regexp
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator,
'/^thumbname-[0-9]*-[0-9]*\.jpg$/i',
RecursiveRegexIterator::GET_MATCH
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With