Monday, July 23, 2007

File Handling - Strip File Extension

This php code will strip the extension from a filename.
to remove the extension just call the function with the name of the file.
function strip_ext($name)
{
$ext = strrchr($name, '.');
if($ext !== false)
{
$name = substr($name, 0, -strlen($ext));
}
return $name;
}
// demonstration
$filename = 'file_name.txt';
echo strip_ext($filename)."\n";
// to get the file extension, do
echo end(explode('.',$filename))."\n";
?>

No comments: