ted_chou12 Posted December 13, 2006 Share Posted December 13, 2006 For example, I have "file.txt", however, what i only want to include is the name part of the file (ie."file"), how shall i get rid of the txt part of the file?Thanks :) Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/ Share on other sites More sharing options...
timmah1 Posted December 13, 2006 Share Posted December 13, 2006 This should do it[code]function findexts ($filename){$filename = strtolower($filename) ;$exts = split("[/\\.]", $filename) ;$n = count($exts)-1;$exts = $exts[$n];return $exts;}[/code] Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/#findComment-140501 Share on other sites More sharing options...
ted_chou12 Posted December 13, 2006 Author Share Posted December 13, 2006 thanks :D Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/#findComment-140502 Share on other sites More sharing options...
papaface Posted December 13, 2006 Share Posted December 13, 2006 Simpler:[code]list($name, $extension) = explode('.', $filename);[/code]$name = without extension$extension = extension Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/#findComment-140504 Share on other sites More sharing options...
timmah1 Posted December 13, 2006 Share Posted December 13, 2006 I have to start thinking more simply :D Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/#findComment-140507 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 That won't work if the filename has more that one period in it.[code]<?php$fn = "test.one.two";list($name,$ext) = explode('.',$fn);echo $name."<br>";echo $ext;?>[/code]will echo "one" for the extension.A better way would be to use the [url=http://www.php.net/basename]basename()[/url] function.[code]<?php$fn = "test.one.two";$pi = pathinfo($fn);echo '<pre>' . print_r($pi,true) . '</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/30523-getting-rid-of-the-file-extension/#findComment-140515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.