Forum Moderators: coopster

Message Too Old, No Replies

Using regular expression in the middle of known variables

php regular expressions variables

         

gwydionmom

9:38 pm on Jan 13, 2007 (gmt 0)

10+ Year Member



I am trying to look through a directory of photos, and find all where the names match a certain pattern. I've been trying variations all day and haven't been able to get anything to work, and unfortunately, didn't keep notes on what I've tried so far. (I'm also relatively new to php, so could easily be making a really silly mistake here.)

To give an example, say the photos I am trying to match will all start with "photo2" It will be followed by a single lower case letter, and then end in jpg. The directory contains these photos:
photo1a.jpg
photo1b.jpg
photo1c.jpg
photo2a.jpg
photo2b.jpg
photo3a.jpg
photo3b.jpg
photo3c.jpg

How do I get photo2a.jpg and photo2b.jpg to come up, but none of the others? ("photo2" will actually be a variable that comes from a mysql query, if that makes any difference.) I've been trying variations using $photoname [a-z] .jpg with eregi to match the pattern, but nothing I've tried so far has worked.

Thank you SO much for any help with this.
Jennifer

gwydionmom

10:38 pm on Jan 13, 2007 (gmt 0)

10+ Year Member



It never fails. I pull out my hair over something all day, and then when I finally decide that I need help, I figure out a solution 2 minutes later.

I ended up using fnmatch instead, and that worked like a charm (could have sworn that I tried it once already, but I guess not). Anyway, if anyone can explain why it worked while eregi didn't, an explanation would be great, but I did get the page to do what I wanted.

Jennifer

eelixduppy

2:36 am on Jan 14, 2007 (gmt 0)



Welcome to WebmasterWorld, Jennifer!

It should work both ways as long as it is done correctly.

Anyway, the most important thing is that you found the solution :)

coopster

1:44 pm on Jan 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if anyone can explain why it worked while eregi didn't, an explanation would be great

You would have to show us the relevant code here first, Jennifer.