Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- RegEx and exclusion
- Mr Phuzz
October 1, 2006, 10:56 pm
rate this thread
I'm pretty new to PHP and am wrestling with regexp's right now. I am
trying to figure something out with no luck. Hopefully someone can
I am trying to filter out file names. I want to write a regexp that
matches all image file names, such as ".jpg", ".bmp", ".png", etc...
but not file names that end with "_thumb.***" (where *** is the
extension of the file name... which can be 3 or 4 chars long). So...
somepic.jpg <--- should match
somepic_thumb.jpg <--- should not match
anotherpic.png <--- should match
this_thumb.is_broken.jpg <--- should match
Currently, I do this using two regexps. First, I compare to determine
if the file is an image by looking at the end and seeing if it's .jpg,
.png, etc. If that passes, I compare again to see if it contains
"_thumb.***" at the end (allowing for 3 or 4 chars, such as jpg or
jpeg), as such:
How can I, if possible, combine these into one regexp?
Thanks for your time,
Re: RegEx and exclusion
But on second thought, many people would prefer two matches (even if it
is a tiny bit less efficient) rather than using such an obscure pattern.
You could then also capture the file type and use it in the second pattern:
if(preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $filename, $match) &&