Jump to content

extract file extension


mpsn

Recommended Posts

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"
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.