The Little Guy Posted December 17, 2006 Share Posted December 17, 2006 I have file names stored in a database, and I was wondering if anyone knew how to get the extension type, basically there will be files like:song.mp3file.mp3cool.jpgawesome.jpgsweet.movfree.see.jpgmy.house.gifthe.song.mp3and so on, I need to get the extension anyone know how, even if there is more than 1 decimal? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted December 17, 2006 Share Posted December 17, 2006 hope this helps[code]<?php$query = mysql_query("SELECT * FROM tablename");$array = mysql_fetch_array($query);if (in_array("mp3", $array) ) { echo("this is a mp3");}if (in_array("jpg", $array) ) { echo("this is a jpg");}?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 Are they always going to be 3 char extensions? Maybe...[code]SELECT SUBSTR(file,-3) AS extension FROM tbl;[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 [quote author=BillyBoB link=topic=118938.msg486474#msg486474 date=1166332536]hope this helps[code]<?php$query = mysql_query("SELECT * FROM tablename");$array = mysql_fetch_array($query);if (in_array("mp3", $array) ) { echo("this is a mp3");}if (in_array("jpg", $array) ) { echo("this is a jpg");}?>[/code][/quote]Well, that simply wont work if there is a file called jpg.gif Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted December 17, 2006 Share Posted December 17, 2006 yea i know thats why you shouldnt use them both in a file... i mean why? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 17, 2006 Author Share Posted December 17, 2006 I was thinking maybe using strrpos, and get the position of the last . then using substr to remove everything before it. Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 Yeah, that'll work. Something like...[code]<?php function getext($filename) { $pos = strpos(strrev($filename),'.'); return substr($filename, -$pos); }?>[/code] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 17, 2006 Author Share Posted December 17, 2006 Here is the function I ended up using:[CODE]function getext($filename) { $pos = strrpos($filename,'.'); $str = substr($filename, $pos); return $str;}[/CODE] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.