webmasteroy Posted August 7, 2007 Share Posted August 7, 2007 Hey, I am have huge problems with this simple problem. I am trying to retrieve the file extension of a file from a string like I have string say "file.php" how do I get the .php out of that with out just saying it is .php like: Say I have a bunch of files that I might be using in actuallity it is infinite. So lets say the files it could be for now are: file.php file.htm file.html file.gif file.jpg file.css file.js If I don't know what the file extension is and I can't think of possible extensions. What would I do to retrieve the file extension on those. I'm thinking of maybe some function that takes the string and cuts off the first section of the string before the dot. Rather then cutting of the extension like strtok($value, ".");. Also you should note my host does not have finfo functions or there is one other function for gathering the mimetypes forgot what it was and can't find it now though. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63726-urgent-retrieving-the-file-extension-from-a-string/ Share on other sites More sharing options...
Crow Posted August 7, 2007 Share Posted August 7, 2007 <?php strrchr($string,'.'); ?> Hope this helps, Steve Quote Link to comment https://forums.phpfreaks.com/topic/63726-urgent-retrieving-the-file-extension-from-a-string/#findComment-317542 Share on other sites More sharing options...
play_ Posted August 7, 2007 Share Posted August 7, 2007 Nice Crow. i've been using: $extension = strtolower( substr($string -3, 3) ); but your way seems alot easier/shorter. the thing you learn browsing through these forums.... Quote Link to comment https://forums.phpfreaks.com/topic/63726-urgent-retrieving-the-file-extension-from-a-string/#findComment-317566 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 I usually use: $file = 'file.doc'; $ext = strtolower(substr(strrchr($file, '.'), 1)); It is basically play_ and Crow's techniques merged togather lol. In this way i get a 'doc' extension without the dot. Quote Link to comment https://forums.phpfreaks.com/topic/63726-urgent-retrieving-the-file-extension-from-a-string/#findComment-318051 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.