mpsn Posted November 20, 2011 Share Posted November 20, 2011 Hi, how do I extract just file extension? eg: $file="hello.xml"; $fileExt=??? print $fileExt; Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/ Share on other sites More sharing options...
QuickOldCar Posted November 20, 2011 Share Posted November 20, 2011 <?php $file="hello.xml"; $fileExt = end(explode(".",$file)); echo $fileExt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/#findComment-1289631 Share on other sites More sharing options...
mpsn Posted November 20, 2011 Author Share Posted November 20, 2011 Thanks, I also found this useful tidbit: $dir=pathinfo("C:\dir\dir2\hello.xml"); print $dir["extension"]; Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/#findComment-1289634 Share on other sites More sharing options...
Fadion Posted November 20, 2011 Share Posted November 20, 2011 As you found out, using pathinfo() is much simpler than a mix of string or vector manipulation functions. It will also be a tad faster, because it uses the internal engine directly. Anyway, you can also find the extension by passing an "options" parameter to pathinfo(), which should be simpler than accessing the vector's key. <?php $file = 'hello.xml'; $ext = pathinfo($file, PATHINFO_EXTENSION); echo $ext; //will output "xml" ?> Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/#findComment-1289637 Share on other sites More sharing options...
mpsn Posted November 20, 2011 Author Share Posted November 20, 2011 Appreciate the constant as second parameter, but what do you mean it is a "tad faster because it uses internal engine", isnt' even php core string manipulation function also part of the internal engine, are you talking about the Zend engine? Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/#findComment-1289638 Share on other sites More sharing options...
Fadion Posted November 20, 2011 Share Posted November 20, 2011 Every PHP function runs by the Zend Engine (internal engine, PHP engine, whatever suits it). My point was that with pathinfo() you run a single PHP function which is interpreted by the Zend Engine and some C code is executed to get the file extension. By using a mix of string or vector manipulation functions, you run 2 or 3 functions which are also interpreted by the engine. The later will normally be slower (even if for not really significant values), because it triggers a greater part of C code in the engine. Usually, achieving something with native functions is faster than with a group of functions not really related to the subject. I think I complicated this more than it really is Quote Link to comment https://forums.phpfreaks.com/topic/251455-extract-file-extension/#findComment-1289651 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.