Solved

PHP/REGEX: Where file names do not have underscore with glob

Posted on 2016-09-29
8
47 Views
Last Modified: 2016-09-30
$f = glob('files/courses/{*_[0-9]*.zip}', GLOB_BRACE);

Open in new window

This matches:
x_123.zip
y_543.zip
z_789.zip

But it does not match:
x123.zip
y543.zip
z789.zip

How can I get it to match:
x123.zip
y543.zip
z789.zip

but not:
x_123.zip
y_543.zip
z_789.zip
?
0
Comment
Question by:skij
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41822162
add a ? after the _
0
 
LVL 10

Author Comment

by:skij
ID: 41822180
I don't want these to match!
x_123.zip
y_543.zip
z_789.zip

If there is a _ before the number then it should not match.
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 50 total points
ID: 41822254
$f = glob('files/courses/{[^_]+[0-9]*.zip}', GLOB_BRACE);

HTH,
Dan
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41822360
{[a-zA-Z0-9]+.zip}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Comment

by:skij
ID: 41822819
Please look carefully at the examples I gave in my original post.

None of the ideas work with the examples provided.
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 150 total points
ID: 41822884
Sorry - I was trying to get back a quick response earlier. I have a few more minutes now and I can tell you that glob() doesn't have a full regex engine, which will make it pretty difficult to do any sort of complex filtering in the future.

So if those are truly the filenames, then this should work:
$f = glob('files/courses/[a-z][0-9]*.zip');

But I'd recommend you set yourself up for success later on and put in a quick use of the full regex engine using preg_match to filter the results that come back:

$f = glob('files/courses/*[0-9]*.zip');
foreach($f as $fk => $fv) { if(!preg_match("/^[a-zA-Z][0-9]+.zip/",basename($fv))) { unset($f[$fk]); } }
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 300 total points
ID: 41823004
glob is actually quite a slow function - especially if you are going to iterate through the results afterwards.
Try the FileSystemIterator instead

$regex = "/^[a-zA-Z]+[0-9]+\.zip$/";
$path = 'files/courses';

$iterator = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME + FilesystemIterator::KEY_AS_FILENAME);
$desirable = array();
foreach ($iterator as $key => $filepath) {
  if (preg_match($regex, $key)) {
    $desirable[] = $filepath;
  }
}

Open in new window

EDIT
Changed to use KEY_AS_FILENAME to remove requirement for basename - 50% faster.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41823007
For interest FSI ran in approx 1/5 of the time of the glob solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now