cgm225 Posted February 16, 2008 Share Posted February 16, 2008 If I have a variable $filename and it contains a filename, so something like: $filename = "movie.avi"; How can I strip off the ".avi" so the variable only equals "movie"? Thank you all in advance! Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/ Share on other sites More sharing options...
thebadbad Posted February 17, 2008 Share Posted February 17, 2008 If the filename never contain a dot, you can do like so: <?php $file = "movie.avi"; list($filename, $ext) = explode('.', $file); // $filename = movie // $ext = avi ?> If it DOES contain dots, the above will fail. Instead you could do this: <?php $file = 'my.movie.avi'; $arr = explode('.', $file); $ext = array_pop($arr); $filename = implode('.', $arr); // $filename = my.movie // $ext = avi ?> Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-468537 Share on other sites More sharing options...
Daniel0 Posted February 17, 2008 Share Posted February 17, 2008 <?php $filename = 'something.somethingElse.ext'; $without_extension = preg_replace('/\.[a-z0-9]+$/i', '', $filename); ?> Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-468685 Share on other sites More sharing options...
dave420 Posted February 18, 2008 Share Posted February 18, 2008 It would be cheaper to use: $name=substr($filename, 0, strrpos($filename, ".")); Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-469709 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2008 Share Posted February 18, 2008 If you're using PHP v5.2 or greater, you can use the pathinfo() function with the PATHINFO_FILENAME constant: <php $filename = 'this.is.a.weird.file.name.longextension'; echo pathinfo($filename,PATHINFO_FILENAME); ?> Ken Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-469728 Share on other sites More sharing options...
dave420 Posted February 18, 2008 Share Posted February 18, 2008 pathinfo is more expensive than both substr and strrpos combined, btw, as it looks up more than just the filename without the extension. Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-469747 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2008 Share Posted February 18, 2008 The OP asked how to do it, not which way was most efficient. What I gave was one more way to do what the OP was looking for. Ken Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-469775 Share on other sites More sharing options...
dave420 Posted February 18, 2008 Share Posted February 18, 2008 It's a great way of doing it - I was simply pointing out a slight consideration one might want to be aware of I wasn't trying to be rude, and I apologise if I seemed that way. Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-469783 Share on other sites More sharing options...
cgm225 Posted February 20, 2008 Author Share Posted February 20, 2008 This has been very helpful! Thank you all so much! (but I still need a topic solved button..) Link to comment https://forums.phpfreaks.com/topic/91448-stripping-off-file-extension-from-filename/#findComment-471764 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.