Reputation: 4349
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
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
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
Reputation: 455000
You can do:
$file_name_no_ext = pathinfo($file_name , PATHINFO_FILENAME);
Upvotes: 2
Reputation: 1488
You could use regular expressions. Regular expression to remove a file's extension
Upvotes: 1