Question on Strings ? How to seperate ext of a image file name from a string ?

How do I get the file ext of a remote image

http://www.somewhere.com/images/myimage.swf

should return  "swf"

http://www.test.com/myimages/banner/468X60.gif

should return "gif"

http://web.images.com/img/banner/test/testing.jpg

should return "jpg"

the urls above are just made up but how do I filter to find the last dot "." then return everything after that?

LVL 2
daveatonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arantiusCommented:
daveaton,

<?
print substr("foo.jpg", strrpos("foo.jpg", "."));
?>

.jpg


Or
<?
print substr("foo.jpg", strrpos("foo.jpg", ".")+1);
?>

jpg
0
y2kwackoCommented:
but what if the string contains test.jpg.exe would it then not return .jpg anyways?

I would strongly suggest using a negative start.

Examples from php manual:

Example 2. Using a negative start

<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>  


$rest = substr($url,-3);  // would return the last 3 characters of the $url variable
0
QuazzMotoCommented:
Or you could go a little long route, but  have the whole url available to you by using

$url = "http://www.test.com/myimages/banner/468X60.gif";

$array = explode("/",$url);  // Explode the URI using '/'.

$num = count($array) - 1;    //get the number -1 for off by 1

$fileArray = preg_split("/[\.]+/",$array[$num]); //split by period, the last one

$num = count($fileArray) - 1;   //in case more than one period

$fileExt = $fileArray[$num];    //set to file extension

echo $fileExt;  //prints gif
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

nicholassolutionsCommented:
Use the simple function below. It either returns the file extension on the url, or FALSE if none is found:

---------------------------------------------------------------------------------
function GetExt($url){
 if (ereg(".([^./]+)$", $url, $matches)) return $matches[1];
 else return FALSE;
}
-------------------------------------------------------------------------------

..and here is how you'd use it:

<?php
$a = "http://www.somewhere.com/images/myimage.swf";
$b = "http://www.test.com/myimages/banner/468X60.gif";
$c = "http://web.images.com/img/banner/test/testing.jpg";

print GetExt($a);  //prints swf
print GetExt($b);  //prints gif
print GetExt($c);  //prints jpg

function GetExt($url){
 if (ereg(".([^./]+)$", $url, $matches)) return $matches[1];
 else return FALSE;
}
?>

Cheers,
Matt
0
nicholassolutionsCommented:
Woops, I forgot a backslash...the function should be:

function GetExt($url){
 if (ereg("\.([^./]+)$", $url, $matches)) return $matches[1];
 else return FALSE;
}


Cheers,
Matt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daveatonAuthor Commented:
Great nicholassolutions this is was exactly what I was looking for, I am using it to filter out what I want to upload to my website, it if does not match it will be rejected, also I will use it to pull the complete url from the database and if it's flash it will be displayed as flash and if it's jpg, gif or png it will be displayed accordingly

I am currently taking classes with zend.com, just finished my first class last week we have not covered the topic  using this ereg("\.([^./]+)$", $url, $matches)  what is this called and do you know a good resource on the web where I can study up on it ? I tried php.net and I can't figure out how the filter works. If you could point me in the right direction I would be greatfull.

Thanks again!
0
nicholassolutionsCommented:
Hi daveaton,

Glad I could be of help. The filter I gave you is an instance of pattern matching using something called regular expressions, which are available in many programming languages, including PHP, Perl, and C.

Here is a link to a pretty good tutorial on getting started with regular expressions. Don't get discouraged if you have some trouble with them at first -- it takes a while to get used to.

http://www.sitepoint.com/article/regular-expressions-php

Good luck!
-Matt
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.