[SOLVED] Multiple Patterns in glob() ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shomizu9
Forum Newbie
Posts: 4
Joined: Thu Jun 17, 2004 11:14 pm

[SOLVED] Multiple Patterns in glob() ?

Post by shomizu9 »

Hey all, first post. :D

Any way to specify multiple patterns in a glob() function?

I've searched high and low all over google, phuse, php.net, this forum, and others, and the closest I've found is another person asking the same question in:

viewtopic.php?t=11420

but his question seems to have been lost/overlooked in the rest of the topic.

I understand the concept of making multiple globs, but surely there's a way to specify multiple patterns in one glob?

For example, say this is my original code:

Code: Select all

<?php

foreach (glob("*.jpg") as $filename)	{

print ("<img src="$filename">\n");
print ("<br>\n");
print ("$filename\n");
print ("<br>\n");
print ("<br>\n");
print ("<br>\n");

}

?>
Now, anyone can see that all this does is print some html to display all image files matching *.jpg, some page breaks, the filename below the image.

What I'd like to be able to do is add *.gif (or anything else) to that same glob as well. I can set up seperate globs for this, but I'd rather not, as the first function to run will display, then the 2nd, and so on, ignoring the natural alphabetical order of the file listing, save where it relates to the given pattern in the given glob.

It would be nice to be able to specify multiple patterns for this reason. I have tried several other language standards for multiple patterns that I have found, but none seem to work (LOL yes I know some of them were silly to try...but hey what could it have hurt/I'm a n00b):

("*.jpg" "*.gif")
("*.jpg *.gif")
({jpg gif})

Is there a format for PHP that I just haven't found in my searches? Or is this just not supported?

Thanks a million.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

foreach (glob("{*.jpg,*.gif}", GLOB_BRACE) as $filename)   {
Look at the user comments at http://php.net/glob for more examples.
shomizu9
Forum Newbie
Posts: 4
Joined: Thu Jun 17, 2004 11:14 pm

Post by shomizu9 »

*smacks forehead*

Of course now I see this flag when I go to that page (I'd been there a couple of times)

The {a,b,c} should have leapt out and clobbered me

Thanks a lot markl999

With only GLOB_BRACE, it has the cumulative effect of 2 defined glob functions, but I'm getting there...I can taste the resolution

if I can just find how to apply multiple flags to the glob...GLOB_NOSORT specifically...

will post more shortly

ps your avatar rules - it's shaken, not stirred :D
shomizu9
Forum Newbie
Posts: 4
Joined: Thu Jun 17, 2004 11:14 pm

Post by shomizu9 »

well, you can add flags together with a simple + between them

however, this did not perform what my n00bish mind thought it would

all images, gif or jpg, did display, however, the jpgs showed up in crazy order, and the gifs did as well, with the gifs still appearing after the jpgs (which would fit the order specified in the brace)

I think I will have to look at other functions again, but this has been an interesting learning experience

the reason I latched onto glob() was due to an fnmatch bit I wrote returning the images in order of *date* rather than the way they appeared in the directory listing LOL

I never could figure that one out
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh... flags are added together with |

eg.

Code: Select all

<?php

glob("*.jpg", GLOB_BRACE | GLOB_ONLYDIR);

// or

preg_split('#(2)#s',$string,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

?>
under somewhat normal circumstances, multiple ones can be added together with the plus, however, in the following case, it'll have unknown results

Code: Select all

<?php

error_reporting(E_ALL + E_NOTICE);

?>
shomizu9
Forum Newbie
Posts: 4
Joined: Thu Jun 17, 2004 11:14 pm

Post by shomizu9 »

thanks, I didn't see that anywhere

the glob performed the same with | or + in:

Code: Select all

<?php

foreach (glob("{*.jpg,*.gif}", GLOB_BRACE+GLOB_NOSORT) as $filename)	{

print ("<img src="$filename"\n");
print ("<br>\n");
print ("$filename\n");
print ("<br>\n");
print ("<br>\n");
print ("<br>\n");

}


?>

// or

<?php

foreach (glob("{*.jpg,*.gif}", GLOB_BRACE | GLOB_NOSORT) as $filename)	{

print ("<img src="$filename"\n");
print ("<br>\n");
print ("$filename\n");
print ("<br>\n");
print ("<br>\n");
print ("<br>\n");

}


?>
I am guessing the GLOB_NOSORT flag just didn't do what I thought it would. I assumed that the images would show up sorted in the same order as the files in the directory listing (alphabetical) after reading:

"Return files as they appear in the directory (no sorting)"

in its description, but that was pretty silly of me really - there are any number of ways a directory listing could be sorted by after all, right?

Thanks for showing me how to add flags together.

*goes back to drawing board*
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in that particular case, those flags will have no difference when added, however you should use the | (bitwise or) operator in the future.

files are stored in a certain order (the order of creation inside directories), the GLOB_NOSORT option, will return how they are listed according to the file system. The sort that happens is an alphabetic sort otherwise.
Post Reply