Preg_match for end of line

I am trying to check if the last characters are $urlone of the following:

.gif .png .bmp .jpg .jpeg

It works great for one of them:

if(!preg_match('/\.jpg$/',$url))

but they all do not work:

if(!preg_match('/[\.gif$\.png$\.bmp$\.jpg$\.jpeg$]/',$url))

What am I doing wrong?

thank

+3
source share
2 answers

You use a character class when you want to alternate ...

"/\.(gif|png|bmp|jpe?g)$/"
+5
source

You cannot put "strings" inside a character class. Character classes work with characters , not strings. A character class can match only one of several characters.

So, the following regular expression:

/[\.gif$\.png$\.bmp$\.jpg$\.jpeg$]/

[ ]. , , \. - . , , .

:

match

. , (foo|bar) foo bar. :

/\.(gif|png|bmp|jpe?g)$/

, . , - ( URL):

$ext = pathinfo($url, PATHINFO_EXTENSION);
+3

All Articles