Jump to content

Recommended Posts

I am using glob to get a list of files in a directory. Problem is, I can't figure out how to exclude certain files with a certain pattern.

 

This grabs all files with ()string in their name:

 

foreach(glob("(*)*.*") as $file)

 

That's works fine (obviously not full code)- but how do I make it now exclude the files with () in?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/
Share on other sites

Sorry I should have mentioned- I've googled for this for about 30 min now, still absolutely nothing. I've checked the forum, I've checked php.net and the documentation.

 

The closest thing I've found is --exclude= but I have no idea if this will work or even how to use it due to not being able to find sufficient examples of it's use.

 

Thanks for the input any way.

Yes, that's exactly what I want to do.

 

The best i have come up with is using an if:

if (preg_match("#((.*?))#", $filename)){
//do this 
} else {
//show file 
}

 

That works flawlessly except it doesn't when I use !preg_match - plus I was hoping there would be some pattern I could include in the actual glob pattern.

 

Thanks.

That's exactly what I thought but I can't get any regex expressions working within the glob...saying that, I'm no expert with regex.

 

Any way, I've resorted to the if statement. For reference:

 

<?php

foreach(glob("*.*") as $file){
if(!preg_match("#\((.*?)\)#", $file)){
///view file
}

?>

 

Thanks for the speedy reponses!

 

  • 5 years later...

Yeah.. glob()'s regex is a bit limited. It has a version of regex that is based off of POSIX but it does not behave the same way. See this comment for some details about how the glob regex works.

 

The short answer is that you CAN just use glob if the parenthesis is at the start or end of the filename, but not within it (but you still can't. See farther below). The core of the reason is that it does not offer ability to quantify a (sub)pattern.

 

For example:

 

Given the string

 

"(foo)bar.html"

 

And the pattern:

 

*[^()]*

 

The first * will first match the whole string. Then it will backtrack until it finds the first character that is not a parenthesis, which is the "l". Then that final * will match nothing since it matches 0 or more of anything.

 

So now let's try this:

 

Given the string

 

"(foo)bar.html"

 

And the pattern:

 

*[^()]

 

This is effectively the same thing as the first one.

 

Now let's try this:

 

Given the string

 

"(foo)bar.html"

 

And the pattern:

 

[^()]*

 

Oh hey, this one works! Since the string starts with a parenthesis, it won't match. So if this is ALL you need, then you should be good to go. But if you need to match for parenthesis within the filename, then glob can't handle it, and you will have to use some php substring matching with the results. Which is basically what you did in the end, except that there are more efficient ways of doing it (e.g. using strpos instead of preg_match).

 

The 2nd thing to consider is filename vs. directory. glob has a flag to only match against directories (exclude files).. but for some dumb reason it does not have a flag to only act against files (exclude directories). So.. in order to only match for FILES, you're going to have to throw some php logic into the mix regardless (using is_dir). Which means there's no real point in using anything more than '*' in the glob and just doing all the filter logic in php.

 

So here is an example of what you can do:

 

foreach(glob('*') as $file) {

  // if not a directory
  if (!is_dir($file)) {

    // if doesn't contain parens
    if  ( (strpos($file,'(')===false)||(strpos($file,')')===false) ) {

      echo $file.'<br/>';

    } // end if not parens

  } // end if not dir

} // end foreach
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.