Computing.Net > Forums > Web Development > wilcard / if or statement

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

wilcard / if or statement

Reply to Message Icon

Name: fcarstens
Date: September 28, 2009 at 22:39:26 Pacific
OS: Windows Vista
Subcategory: PHP
Comment:

Hi there... I don't know anything about php, so don't even know what to search for to get the right answer...

I have this piece of code
$image = 'logo'.$_GET['im'].'.jpg';

problem is, the image can either be a jpg or a gif or even a png. How can I change this line to display the logo whether it's a gif, jpg, or png? Is it possible to use some sort of a wildcard?
the name will always be the same: logo[im].*



Sponsored Link
Ads by Google

Response Number 1
Name: Radix-64
Date: October 2, 2009 at 15:01:39 Pacific
Reply:

Hi, not sure if there's a wild card that you can use.

One approach would be to extract the file extension, then process it to display the image. Perhaps here's one way on how to identify the file extension:

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}


0

Response Number 2
Name: shutat
Date: October 3, 2009 at 02:05:22 Pacific
Reply:

Another idea may be something like

<?php

   $image = file_exists('logo' . $_GET['im'] . '.jpg') ?
            'logo' . $_GET['im'] . '.jpg' :
            file_exists('logo' . $_GET['im'] . '.gif') ? 
            'logo' . $_GET['im'] . '.gif' :
            file_exists('logo' . $_GET['im'] . '.png') ?
            'logo' . $_GET['im'] . '.png' : null;

   if(!is_null($image)) {
      echo "<img src='" . $image . "'>";
   }
?>

You might also try using glob for wildcards

<?php

   foreach(glob('path/logo' . $_GET['im'] . '.*') as $fname) {
      echo "<img src='" . $fname . "'>";
   }
?>

The glob would produce multiple images if more than one were found or perhaps an error if the directory was empty, so you'd want to modify it as needed.

HTH


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: wilcard / if or statement

nested if else statements www.computing.net/answers/webdevel/nested-if-else-statements/3757.html

parsing sequence of DHTML-form www.computing.net/answers/webdevel/parsing-sequence-of-dhtmlform/2209.html

Time For A Change... www.computing.net/answers/webdevel/time-for-a-change/104.html