tags:

views:

87

answers:

2

Heya

im looking to validate a string and make sure it is the following format

"thumb_*.jpg|gif|png"

where * is a wildcard, and jpg|gif|png are optional file extensions

this is in PHP

Is that possible?

ps: i just want return value true or false

+2  A: 
$valid = preg_match('~^thumb_.*\.(jpg|gif|png)$~', $filename) != 0;

Or if you really want the extension to be optional:

$valid = preg_match('~^thumb_.*(\.(jpg|gif|png))?$~', $filename) != 0;

EDIT: As per Am's comment on Cletus' answer, you might want to restrict the wildcard to avoid matching slashes:

$valid = preg_match('~^thumb_[^/]*\.(jpg|gif|png)$~', $filename) != 0;
Max Shawabkeh
A: 
if (preg_match('/^thumb_.*?\.(jpg|gif|png)$/i', $filename)) {
  ...
}
cletus
This will allow bad filenames, and maybe even a security threat (like __../../../__). You should restrict the .* to specific characters.
Am
hey buddy. any reason why this $filename isn't returning true? "thumb_abc.jpg"
It won't return `true`. It will return `1`, i.e. the number of time it matched.
Max Shawabkeh