DamienRoche Posted October 12, 2008 Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/ Share on other sites More sharing options...
Bendude14 Posted October 12, 2008 Share Posted October 12, 2008 Look into regular expressions... Sorry i can't tell you exactly the pattern you need but im sure a bit of googling will reveal the answer Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663236 Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Author Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663237 Share on other sites More sharing options...
Zane Posted October 12, 2008 Share Posted October 12, 2008 you want to exclude file with parentheses in them? am I correct Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663239 Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Author Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663243 Share on other sites More sharing options...
Zane Posted October 12, 2008 Share Posted October 12, 2008 foreach(glob("*/[^\(\)].*") as $file) not positive that will work, but give it a shot EDIT: if not that then maybe just this foreach(glob("[^\(\)].*") as $file) Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663247 Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Author Share Posted October 12, 2008 That doesn't work. This is the trouble I've been having. It doesn't seem to function as a regex. Any other ideas? Thanks any way, I'll do some more experiementing with that. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663248 Share on other sites More sharing options...
Zane Posted October 12, 2008 Share Posted October 12, 2008 Well as far as I know the glob function uses the POSIX engine but I'm not entirely sure. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663249 Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Author Share Posted October 12, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-663250 Share on other sites More sharing options...
.josh Posted October 28, 2013 Share Posted October 28, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-1455930 Share on other sites More sharing options...
Zane Posted October 29, 2013 Share Posted October 29, 2013 If there was ever a correct way to necro a thread, I would have to say you nailed it CV. Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-1455977 Share on other sites More sharing options...
.josh Posted October 29, 2013 Share Posted October 29, 2013 huh..well that's weird.. only reason I even went to this thread was cuz it was on the first page of the thread listing.. what trickery is this?!? Quote Link to comment https://forums.phpfreaks.com/topic/128074-solved-how-to-exclude-using-glob/#findComment-1455982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.