Jump to content

Recommended Posts

Hey all

 

I have a line in my script that strips the file extension from the file name:

 

$ext = substr($filen, strpos($filen,'.'), strlen($filen)-1);

 

But this is not fool proof as I've noticed that some files contain more than one period. For example:

 

mysong_feat._artist.mp3

 

The code above would read the $ext variable as being ._ar instead of .mp3.

 

The strpos function finds the position of the first occurrence of a string.  :(

 

Is there a way to make it find the position of the last occurrence of a string? Or is there a different function I should be using?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/140160-solved-isolating-a-filename-extension/
Share on other sites

Why not just pull the last 3 characters?

 

$ext = substr("file.ext.ext.php", -3);

 

Or, why not explode or split at the period and pull the last element of the array:

$ext = explode(".", "file.ext.ext.php");
$ext = $ext[count($ext)-1];

 

Or pathinfo

 

$path_parts = pathinfo('file.ext.ext.php');
$ext = $path_parts['extension'];

Why not just pull the last 3 characters?

 

$ext = substr("file.ext.ext.php", -3);

 

Or, why not explode or split at the period and pull the last element of the array:

$ext = explode(".", "file.ext.ext.php");
$ext = $ext[count($ext)-1];

 

Or pathinfo

 

$path_parts = pathinfo('file.ext.ext.php');
$ext = $path_parts['extension'];

 

Don't just pull last 3 chars, extension could be 1, 2 or 4+ chars.

 

explode works, just do some error checking to make sure that there is a '.' or else you will just end up returning the filename.

 

pathinfo works, but I think it suffers from the same problem as the explode method if the file has no extension.

Here is a pretty concise bit of code I found:

<?php
$filename = 'pterodactyl.wtf';
$ext = substr(($t = strrchr($filename, '.')) !== false ? $t : '', 1);
?>

 

In function form:

<?php
function get_file_extension($filename) 
{
    return substr(($t = strrchr($filename, '.')) !== false ? $t : '', 1);
}
?>

Why not just pull the last 3 characters?

 

$ext = substr("file.ext.ext.php", -3);

 

Or, why not explode or split at the period and pull the last element of the array:

$ext = explode(".", "file.ext.ext.php");
$ext = $ext[count($ext)-1];

 

Or pathinfo

 

$path_parts = pathinfo('file.ext.ext.php');
$ext = $path_parts['extension'];

 

Don't just pull last 3 chars, extension could be 1, 2 or 4+ chars.

 

explode works, just do some error checking to make sure that there is a '.' or else you will just end up returning the filename.

 

pathinfo works, but I think it suffers from the same problem as the explode method if the file has no extension.

 

$path_parts = pathinfo('file.ext.ext.php');
$ext = isset($path_parts['extension'])?$path_parts['extension']:'';

 

For explode:

$ext = explode(".", "file.ext.ext.php");
$ext = (count($ext)>1)?$ext[count($ext)-1]:'';

 

That would avoid any errors of no path being passed I would believe.

Use end :)

 

And won't explode return an array even if there is only one element?

 

$ext = explode(".", "file.ext.ext.php");
$ext = (count($ext) > 1) ? end($ext) : '';

 

Yea I just did my testing and found that out =) And I guess I never looked for a function like end good to know =P

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.