daveaton
asked on
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?
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?
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
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
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("/[\.]+/",$arra y[$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
$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("/[\.]+/",$arra
$num = count($fileArray) - 1; //in case more than one period
$fileExt = $fileArray[$num]; //set to file extension
echo $fileExt; //prints gif
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
--------------------------
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
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
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
<?
print substr("foo.jpg", strrpos("foo.jpg", "."));
?>
.jpg
Or
<?
print substr("foo.jpg", strrpos("foo.jpg", ".")+1);
?>
jpg