?
Solved

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

Posted on 2012-08-31
4
Medium Priority
?
666 Views
Last Modified: 2012-09-16
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
Comment
Question by:hankknight
4 Comments
 
LVL 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 536 total points
ID: 38355325
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
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 532 total points
ID: 38357018
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 532 total points
ID: 38357357
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
 
LVL 27

Accepted Solution

by:
skullnobrains earned 400 total points
ID: 38401879
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question