papaface Posted December 9, 2006 Share Posted December 9, 2006 Hello,I have seen this type of topic answered before but I cant seem to find it.Therefore could someone kindy tell me how I would go about separating the extension from the filename, AND have the ability to echo them both out.Currently I have:[code]$filename = pathinfo("text.txt")echo $filename["extension"];echo $filename["filename"];[/code]But this doesnt work on some versions of mysql/php.Thanks. Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/ Share on other sites More sharing options...
Daniel0 Posted December 9, 2006 Share Posted December 9, 2006 [code]$filename = explode('.',$filename);$extension = $filename[count($filename)-1];$filename = array_pop($filename);$filename = join('.',$filename);[/code] Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137827 Share on other sites More sharing options...
papaface Posted December 9, 2006 Author Share Posted December 9, 2006 Thanks, how would I go about echoing the filename and the extention using that? Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137832 Share on other sites More sharing options...
marcus Posted December 9, 2006 Share Posted December 9, 2006 As seen in Daniel's post, $filename = explode(etc...)You can easily set:[code]$fn = $filename[0];[/code]then echo off $fn Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137833 Share on other sites More sharing options...
papaface Posted December 9, 2006 Author Share Posted December 9, 2006 I get[code]Warning: join() [function.join]: Bad arguments. in split.php on line 6[/code][code]<?php$filename = "text.txt";$filename = explode('.',$filename);$extension = $filename[count($filename)-1];$filename = array_pop($filename);$filename = join('.',$filename);$fn = $filename[0];echo $fn;?>[/code] Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137834 Share on other sites More sharing options...
redbullmarky Posted December 9, 2006 Share Posted December 9, 2006 [code]<?php$filename = "text.txt";list($the_file_bit, $extension) = explode('.', $filename);echo "here's the file bit: $the_file_bit and here's the extension: $extension";// or :echo $the_file_bit;echo $extension;?>[/code]have a look at the [url=http://www.php.net/list]list[/url] function. just the ticket for this type of thing.cheersMark Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137847 Share on other sites More sharing options...
papaface Posted December 9, 2006 Author Share Posted December 9, 2006 Thank you, that is perfect. Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137849 Share on other sites More sharing options...
roopurt18 Posted December 9, 2006 Share Posted December 9, 2006 IMO you can't assume1) that a file has an extension2) that a file doesn't have more than one dot in itHere's what I would use:[code]<?phpfunction filebits($file){ $bits = Array( 'name' => NULL, 'ext' => NULL ); if(strlen($file)){ $file = explode(".", $file); $bits['name'] = $file[0]; $bits['ext'] = ($num = count($file)) > 1 ? $file[$num - 1] : NULL; } return $bits;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/29992-separate-extension-from-filename/#findComment-137886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.