Re: I'm confused about Regular Expressions.
| From: | Christian Reiniger | Date: | Thu, 22 Feb 2001 10:39:24 +0000 |
| Subject: | Re: I'm confused about Regular Expressions. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-41121@lists.php.net to get a copy of this message | ||
On Wednesday 21 February 2001 18:42, Kenneth R Zink II wrote:
> > I was just reading at DevShed on how to use Regular Expressions, but
> now I'm even more confused.
>
> Can someone please explain how I would use a REGEX to determine in and
> uploaded file is a .gif or .jpg file?
if (preg_match ('/\.(gif|jpg)$/i', $filename, $matches))
{
echo "Congratulations, it's a " . $matches [1] . "!<br>";
}
else
{
echo "Wrong file type!<br>";
}
Explanation:
/.../ are the "regular expression quotes", i.e. the real expression is
between these slashes
The "i" after them means "do a case insensitive match" (a "pattern
modifier")
\.(gif|jpg) means "match if the string contains a dot followed by either
"gif" or "jpg""
The dot has to be escaped (prepended with a backslash) because a dot in a
regexp has a special meaning: "match any char". The escaping makes it
behave as a literal dot
The "$" means "here has to be the end of the string" (an "anchor")
=> the entire pattern means
"match if the string ends with a dot followed by either 'gif' or 'jpg',
and ignore case for this"
--
Christian Reiniger
LGDC Webmaster (http://sunsite.dk/lgdc/)
I saw God --------- and she was black.