PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > RE: [PHP] newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
RE: [PHP] newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/08/2008, 19h08   #1
Ford, Mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?

On 26 August 2008 17:15, James Ausmus advised:

> 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...


The problem with that is that a file called, say, site.logo.jpg will
fail the subsequent tests, since the substr() on the next line will
return '.logo.jpg'. (And whilst it is vanishingly improbable, it is
_just_ possible for someone to supply a file called .gif !!!!!

Cheers!

Mike

--
Mike Ford, Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
  Réponse avec citation
Vieux 26/08/2008, 19h36   #2
James Ausmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?

On Tue, Aug 26, 2008 at 10:08 AM, Ford, Mike <M.Ford@leedsmet.ac.uk> wrote:
> On 26 August 2008 17:15, James Ausmus advised:
>
>> 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...

>
> The problem with that is that a file called, say, site.logo.jpg will
> fail the subsequent tests, since the substr() on the next line will
> return '.logo.jpg'. (And whilst it is vanishingly improbable, it is
> _just_ possible for someone to supply a file called .gif !!!!!
>


Very true - shows what a couple seconds of thought prior to full
caffeine will give you.

However, it still does illustrate a concept that is important - making
PHP's loose typing work *for* you, instead of fighting against it -
many of the posts here have approximately said "DANGER! Loose typing
ahead!", which could tend to make a new PHP programmer only think of
the loose typing in a negative form - something to watch out for and
avoid. If, however, you internalize the implications of loose typing,
then you can actually make good use of it, instead of just guarding
against it like the plague.

-James


> Cheers!
>
> Mike
>
> --
> Mike Ford, Electronic Information Developer,
> C507, Leeds Metropolitan University, Civic Quarter Campus,
> Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
> Email: m.ford@leedsmet.ac.uk
> Tel: +44 113 812 4730
>
>
> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

  Réponse avec citation
Vieux 30/08/2008, 00h20   #3
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] newbie Q: How to say, "if the fileNAME is equal to...",or better yet, "if the fileNAME ends with '.jpg'"?

James Ausmus schreef:
> On Tue, Aug 26, 2008 at 10:08 AM, Ford, Mike <M.Ford@leedsmet.ac.uk> wrote:
>> On 26 August 2008 17:15, James Ausmus advised:
>>
>>> 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...

>> The problem with that is that a file called, say, site.logo.jpg will
>> fail the subsequent tests, since the substr() on the next line will
>> return '.logo.jpg'. (And whilst it is vanishingly improbable, it is
>> _just_ possible for someone to supply a file called .gif !!!!!
>>

>
> Very true - shows what a couple seconds of thought prior to full
> caffeine will give you.
>
> However, it still does illustrate a concept that is important - making
> PHP's loose typing work *for* you, instead of fighting against it -
> many of the posts here have approximately said "DANGER! Loose typing
> ahead!", which could tend to make a new PHP programmer only think of
> the loose typing in a negative form - something to watch out for and
> avoid. If, however, you internalize the implications of loose typing,
> then you can actually make good use of it, instead of just guarding
> against it like the plague.


very good point.

indeed it should be noted loosetyping is a good thing, you just need
to understand how to use it diligently. anyone who thinks it's evil
can always f*** off to the java camp :-P

>
> -James
>
>
>> Cheers!
>>
>> Mike
>>
>> --
>> Mike Ford, Electronic Information Developer,
>> C507, Leeds Metropolitan University, Civic Quarter Campus,
>> Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
>> Email: m.ford@leedsmet.ac.uk
>> Tel: +44 113 812 4730
>>
>>
>> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 15h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11919 seconds with 11 queries