• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

PHP: Get files with higher number in filename using glob()

Using PHP's function glob(), how can I get all files that contain a number greater than 12 in a specific part of the filename?

$m = glob('xyz/*/Item_[ Number Greater Than 12  ].txt');

Open in new window

0
hankknight
Asked:
hankknight
4 Solutions
 
Chris StanyonCommented:
Not sure you can do it with just glob. You may need to throw in a preg_match:

$files = glob('*.txt');
$matchingFiles = array();
    foreach ($files as $file) {
		if (preg_match("/Item_([0-9])\.txt/",$file,$matches)) {
			if ($matches[1] > 12) { $matchingFiles[] = $file; }	
		}
    }
var_dump($matchingFiles);

Open in new window

0
 
Mohamed AbowardaSoftware EngineerCommented:
The idea is that you go through each file and check if the file contains a number more than 12.

Take a look over scandir():
http://php.net/manual/en/function.scandir.php
0
 
Ray PaseurCommented:
It cannot be done with glob() alone.
http://php.net/manual/en/function.glob.php

If you want to post a list of file names that are representative of your directory, I'll be glad to write a function to return an array of those that match your criteria.
0
 
skullnobrainsCommented:
it can be done with glob providing that you know the maximum number of digits of the number or providing that you can accept junk after the number

the pattern would be like this :
'xyz/*/Item_{1[2-9],[0-9][0-9]}*.txt'

or like this if you do not want junk, but know the number of digits does not exceed 4
'xyz/*/Item_{1[2-9],[0-9][0-9],[0-9][0-9][0-9],[0-9][0-9][0-9][0-9]}.txt'

and you need the flag GLOB_BRACE (please check the exact name of the flag i'm not sure)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now