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! Quote Link to comment 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 ?> Quote Link to comment 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); ?> Quote Link to comment 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, ".")); Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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..) 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.