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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.