sh0wtym3 Posted January 9, 2009 Share Posted January 9, 2009 Hey all I have a line in my script that strips the file extension from the file name: $ext = substr($filen, strpos($filen,'.'), strlen($filen)-1); But this is not fool proof as I've noticed that some files contain more than one period. For example: mysong_feat._artist.mp3 The code above would read the $ext variable as being ._ar instead of .mp3. The strpos function finds the position of the first occurrence of a string. Is there a way to make it find the position of the last occurrence of a string? Or is there a different function I should be using? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/ Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Try this: (untested) <?php function get_file_ext($filename) { if (($pos = strrpos($filename, '.')) !== false) { return substr($filename, $pos, strlen($filename) - 1); } else { return ''; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733426 Share on other sites More sharing options...
JonnoTheDev Posted January 9, 2009 Share Posted January 9, 2009 explode the file string into an array. The file type will be the last element $filename = "mysong_feat._artist.mp3"; $parts = explode(".", $filename); print_r($parts); Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733427 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 Why not just pull the last 3 characters? $ext = substr("file.ext.ext.php", -3); Or, why not explode or split at the period and pull the last element of the array: $ext = explode(".", "file.ext.ext.php"); $ext = $ext[count($ext)-1]; Or pathinfo $path_parts = pathinfo('file.ext.ext.php'); $ext = $path_parts['extension']; Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733428 Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Why not just pull the last 3 characters? $ext = substr("file.ext.ext.php", -3); Or, why not explode or split at the period and pull the last element of the array: $ext = explode(".", "file.ext.ext.php"); $ext = $ext[count($ext)-1]; Or pathinfo $path_parts = pathinfo('file.ext.ext.php'); $ext = $path_parts['extension']; Don't just pull last 3 chars, extension could be 1, 2 or 4+ chars. explode works, just do some error checking to make sure that there is a '.' or else you will just end up returning the filename. pathinfo works, but I think it suffers from the same problem as the explode method if the file has no extension. Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733431 Share on other sites More sharing options...
sh0wtym3 Posted January 9, 2009 Author Share Posted January 9, 2009 Thanks guys, wasn't aware I could do all that, I'm still fairly new to php. I'm going to explode the string (at the period) and pull the last element, makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733433 Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Here is a pretty concise bit of code I found: <?php $filename = 'pterodactyl.wtf'; $ext = substr(($t = strrchr($filename, '.')) !== false ? $t : '', 1); ?> In function form: <?php function get_file_extension($filename) { return substr(($t = strrchr($filename, '.')) !== false ? $t : '', 1); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733435 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 Why not just pull the last 3 characters? $ext = substr("file.ext.ext.php", -3); Or, why not explode or split at the period and pull the last element of the array: $ext = explode(".", "file.ext.ext.php"); $ext = $ext[count($ext)-1]; Or pathinfo $path_parts = pathinfo('file.ext.ext.php'); $ext = $path_parts['extension']; Don't just pull last 3 chars, extension could be 1, 2 or 4+ chars. explode works, just do some error checking to make sure that there is a '.' or else you will just end up returning the filename. pathinfo works, but I think it suffers from the same problem as the explode method if the file has no extension. $path_parts = pathinfo('file.ext.ext.php'); $ext = isset($path_parts['extension'])?$path_parts['extension']:''; For explode: $ext = explode(".", "file.ext.ext.php"); $ext = (count($ext)>1)?$ext[count($ext)-1]:''; That would avoid any errors of no path being passed I would believe. Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733437 Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Use end And won't explode return an array even if there is only one element? $ext = explode(".", "file.ext.ext.php"); $ext = (count($ext) > 1) ? end($ext) : ''; Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733441 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 Use end And won't explode return an array even if there is only one element? $ext = explode(".", "file.ext.ext.php"); $ext = (count($ext) > 1) ? end($ext) : ''; Yea I just did my testing and found that out =) And I guess I never looked for a function like end good to know =P Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733444 Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Sorry for continually posting, but I think I found a winner: <?php $filename = 'mypic.gif'; $ext = pathinfo($filename, PATHINFO_EXTENSION); ?> Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733446 Share on other sites More sharing options...
sh0wtym3 Posted January 9, 2009 Author Share Posted January 9, 2009 Its ok flyhoney, I appreciate all the assistance. Quote Link to comment https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/#findComment-733479 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.