Josh979
Josh979

Reputation: 361

Can glob be used to exclude only filenames containing letter characters?

I'm trying to count all files (all of which are images) in a folder for an image gallery. In this folder I have photos named 1.jpg, 2.jpg, 3.jpg, and so on. However, there are a few photos in this folder that have a letter in the filename (example: filename is 12b.jpg or 12c.jpg) in the same folder, but I don't want them to be counted as a file/image in my counting script.

Can I use PHP's glob function to exclude filenames containing a letter, or perhaps include only filenames containing purely numbers?

I think I can use REGEX to do this, but I'm not familiar enough with it to know how to exclude anything other than specific characters, rather than restricting a type of character (ie letters).

Current Code is:

// Get Exterior Image Count
$exteriorImgDirectory = "images/exterior-photos/";
if (glob($exteriorImgDirectory . "*.jpg") != false)
{$exteriorCount = count(glob($exteriorImgDirectory . "*.*"));}

Upvotes: 0

Views: 800

Answers (1)

ThinkTank
ThinkTank

Reputation: 1191

Just create the right regular expression. Something like this should work.

glob($exteriorImgDirectory . "[0-9]+.jpg")

If you want to test regular expression, there is some usefull site. An example : Rubular

Upvotes: 1

Related Questions