acctman
acctman

Reputation: 4349

removing last 3 characters on a file (file extension)

my file name are being stored in a variable $file_name... how can i remove the extension and just have the name only? is there something other than strcmp that i can use... that doesn't seem to do it

Upvotes: 2

Views: 997

Answers (7)

David Weinraub
David Weinraub

Reputation: 14184

Similar to @fire, but more robust in the face of multiple dots:

$comps = explode('.', $filename);
unset($comps[count($comps) - 1]);
$ext = implode('.', $comps);

Upvotes: 0

anon
anon

Reputation:

You should take a look at the pathinfo() function.

Upvotes: 1

Alin P.
Alin P.

Reputation: 44346

Use pathinfo.

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>

Note: If you're not on PHP >= 5.2 use this to compose the filename

$path_parts['filename'] = substr($path_parts['basename'], 0, -1*strlen($path_parts['extension'])-1);

Upvotes: 9

codaddict
codaddict

Reputation: 455000

You can do:

$file_name_no_ext = pathinfo($file_name , PATHINFO_FILENAME);

Upvotes: 2

Pit
Pit

Reputation: 1488

You could use regular expressions. Regular expression to remove a file's extension

Upvotes: 1

Luca Bernardi
Luca Bernardi

Reputation: 4199

substr($file_name, 0, -4);

Upvotes: 1

fire
fire

Reputation: 21531

I use:

$file_name = current(explode(".", $file_name));

Upvotes: -3

Related Questions