0

how can fix it or add regexp ?

case substr($rrr['url'],-4)=='.jpg' || '.png' || '.gif' || '.tif' || '.tiff': 
2
  • Is the check case sensitive? http://example.com/icon.JPG What about query parameters? http://example.com/icon.jpg?src=search What about other pages? http://example.com/evil#justlookslegit.jpg
    – Kobi
    Commented Apr 6, 2011 at 8:58
  • Try strrchr($rrr["url"], ".") to get the extension.
    – mario
    Commented Apr 6, 2011 at 22:27

4 Answers 4

5

Something like this?

case in_array(substr($rrr['url'],-4), array('.jpg','.png','.gif','.tif')):
case in_array(substr($rrr['url'],-5), array('.tiff')):

Note, that I omit break; between the case-expression.

Also cool:

case in_array(pathinfo($rrr['url'], PATHINFO_EXTENSION), array('jpg','png','gif','tif', 'tiff')):

The snippet from the question doesnt work, because its evaluated into (shortened)

(substr($rrr['url'],-4)=='.jpg') || '.png'

This works, but oviously it doesnt make much sense and is very probably not, what is expected.

Update: This solution seems much cleaner. It assumes, that $rrr['url'] is the only interesting here. See comments

switch (pathinfo($rrr['url'], PATHINFO_EXTENSION)):
  case 'jpg':
  case 'png':
  case 'gif':
  case 'tif':
  case 'tiff':
    do_something();
  break;
}
9
  • If this is indeed used as a switch case, this will not work like you expect it to work. If it works at all, it's just because the result of that expression happens to loosely compare to the switch comparison value.
    – deceze
    Commented Apr 6, 2011 at 9:07
  • @deceze: The questioner omitted the switch($expr)-part, so I had to guess. Maybe its just switch(true).
    – KingCrunch
    Commented Apr 6, 2011 at 9:17
  • @King Sure, but that would be just as weird and needs pointing out.
    – deceze
    Commented Apr 6, 2011 at 9:21
  • @deceze: Hmm.. valid point. Maybe I guessed a little bit too much. But what is weird with switch(true)?
    – KingCrunch
    Commented Apr 6, 2011 at 9:24
  • 2
    @King IMO switch(true) is abusing the switch statement and would be better replaced with an if. Maybe that's just me though.
    – deceze
    Commented Apr 6, 2011 at 9:26
4
  1. $foo == A || B || C doesn't work, this needs to be $foo == A || $foo == B || $foo == C or in_array($foo, array(A, B, C)).
  2. You can't have complex cases within switch statements. Each case can only have one value against which the comparison value will be compared. You'd have to write this as separate fall-through cases:

    switch ($foo) {
        case A :
        case B :
        case C :
            bar();
            break;
    }
    
0

You cant use A == B || C || D statement for that, only A == B || A == C || A == D

Also, urls can have GET parameters.

0

You need to find strrpos of dot, get substring after rightest dot position (which returned by strrpos), define array of allowed extensions (it also makes your code reusable), and then use in_array like this:

$rpos = strrpos($rrr['url'], '.');
$ext = substr($rrr['url'], $rpos+1);
$allowedExtensions = array('jpg','png', 'gif', 'tif', 'tiff');
///....
if (in_array($ext, $allowedExtensions)) {
///....

Not the answer you're looking for? Browse other questions tagged or ask your own question.