Re: newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?

From: Date: Tue, 26 Aug 2008 16:15:10 +0000
Subject: Re: newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-278732@lists.php.net to get a copy of this message
On Tue, Aug 26, 2008 at 8:57 AM, Ford, Mike <M.Ford@leedsmet.ac.uk> wrote: > On 25 August 2008 00:54, Govinda advised: <snip> > Personally, I might be tempted to do something like this: > > if (($pos = strrchr($file, '.'))!==FALSE): > switch (strtolower(substr($file, $pos))): > case '.gif': > case '.png': > case '.jpg': > case '.jpeg': > echo $file; > endswitch; > endif; <snip> Of course, this could be simplified *slightly* by actually taking advantage of the loose typing of PHP - change the above if statement to: if (($pos = strpos($file, '.'))) { $restOfAboveCodeHere; } This way, if the file is the '.' or '..' files, then you will get a 0 for the position of the '.', which will evaluate to FALSE. All other files will return a non-zero position of the '.' char, which will evaluate to TRUE. Also, I would believe (but have no evidence at all of) that a forward-looking string position search will be very slightly faster than a strrchr search - dunno for certain, and don't care enough to code up a couple-line test, but just my gut... ;) -James

« previous php.general (#278732) next »