...

View Full Version : printing the filename with out extension



mrjameer
03-21-2007, 08:49 PM
hi,


i want to print the file name with out extension.here is my code for getting the extension.but how i can get only file name without extension

<?php
$file = "welcome.txt";
$aFile = explode(".", $file);
$ext = array_pop($aFile);
echo $ext;
?>

any of your idea will be appreciated.

thanks
mrjameer.

Nightfire
03-21-2007, 09:03 PM
<?php
$file = "welcome.txt";
$aFile = explode(".", $file);
$filename = $aFile[0];
$ext = $aFile[1];
echo $filename .'<br />'.$ext;
?>

aedrin
03-21-2007, 09:05 PM
$file = "/path/to/file.txt";

$filename = substr(strrchr($file, '/'), 1);
$extension = substr(strrchr($filename, '.'), 1);
$filename_no_ext = substr(strrchr($filename, '.'), 1);

mrjameer
03-21-2007, 10:28 PM
hi,

thank you very much all of you.it works nice.


thanks
mrjameer.

ralph l mayo
03-21-2007, 10:44 PM
http://us2.php.net/manual/en/function.pathinfo.php

fackelkind
03-22-2007, 02:11 AM
How do you like that one:



<?php #覧[func.filename.php]覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧/
#> Extracts the fileame from a path #
#> #
#> Changelog: #
#> v1.1 [ADD] Support for .htaccess files (shows .htaccess instead of nothing) #
#> v1.0 Initial Release #
#> ___________________________ #
#覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧余 fackelkind | codeMaster [覧#
// (c) 2oo7 - ICQ: 34o 839 296 ッッッッッッッッッッッッッッッッッッッッッッッッッッッ \\

#>覧 the function:
function filename ($Path){
#> (string) filename ((string) Pathname)
return preg_replace ("%^(?:^.*\/|^)(.+?)(?=\.[^\.]+|$).*%s", "$1", $Path);
}

#>覧 examples:
# filename ("http://www.honorsociety.de/path/script.php") => "script"
# filename ("/path/script.php") => "script"
# filename ("script.php") => "script"
# filename ("script") => "script"

#>覧 EOF (1099 Bytes - Mar 22nd 01:09 2007)
?>

mrjameer
03-25-2007, 04:21 PM
hi,

how i can handle the files whose extension is like abcd.tar.gz
here i want to print the file name as abcd.tar and extension is gz.

thanks
mrjameer

rafiki
03-25-2007, 04:29 PM
using nightfires method would be something like this

<?php
$file = "welcome.txt";
$aFile = explode(".", $file);
$filename = $aFile[0];
$ext = $aFile[1];
if (!empty($aFile[2]));
{
$ext2 = $aFile[2];
echo $filename .'<br />'.$ext .$ext2;
}else{
echo $filename .'<br />'.$ext;
}

?>

koyama
03-25-2007, 05:35 PM
Did you look at the link that ralph|mayo gave you?


$file = '/path/to/abcd.tar.gz';

$a = pathinfo($file);

// result:
$a['dirname'] = '/path/to'
$a['basename'] = 'abcd.tar.gz'
$a['extension'] = 'gz'
$a['filename'] = 'abcd.tar'

maghiel
03-26-2007, 01:15 PM
yeah indeed, just pathinfo()
why are so many people messing with regexps and stuff????
pathinfo works just fine!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum