?
Solved

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

Posted on 2004-11-22
7
Medium Priority
?
310 Views
Last Modified: 2008-03-06
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?

0
Comment
Question by:daveaton
7 Comments
 
LVL 18

Expert Comment

by:arantius
ID: 12651363
daveaton,

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

.jpg


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

jpg
0
 
LVL 3

Expert Comment

by:y2kwacko
ID: 12651404
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
 
LVL 1

Expert Comment

by:QuazzMoto
ID: 12651429
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12651537
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
 
LVL 15

Accepted Solution

by:
nicholassolutions earned 200 total points
ID: 12651561
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
 
LVL 2

Author Comment

by:daveaton
ID: 12652128
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
 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12652153
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question