View Full Version : Resolved Syntax for Wildcard in String?

11-16-2010, 04:37 AM
I have a script that looks through a directory and displays files with either the extension jpg or gif. I want to add to that criteria that it should not display any file with a name that begins with "ref_".

I am not getting any results using * as a wildcard replacement for the rest of the file name. If I replace the wildcard * with a precise filename, the script works fine.

Can anyone advise as to what is wrong with my syntax? It looks just like dozens of examples I've seen used elsewhere, including a few on this forum. So I'm rather puzzled.

Thank you for any help you can provide. :)


// open the directory
$dir = opendir( $pathToImages );

// parse path
$info = pathinfo($pathToImages . $fname);

//strips extension off file name for later use
//assumes file extension is three characters
$pos = strrpos($fname, '.');
$basename = substr($fname, 0, $pos);
$extension = substr($fname, $pos+1);

// strip the . and .. entries off and parse for valid jpg or gif
if ($basename != 'ref_' . "*" && (strtolower($info['extension']) == 'jpg') || (strtolower($info['extension']) == 'gif'))

11-16-2010, 04:40 AM

if ($basename != 'ref_' . "*" &&


if (strpos($basename, 'ref_') === false &&

That will match if ref_ is anywhere in the filename. To limit it to the start only, you would need:

if (!preg_match('~^ref_~i', $basename) &&

11-16-2010, 04:49 AM
Good grief, thank you - I spent waaaay too much time trying to sort that out on my own. I used that last solution and it worked precisely as desired. :)