ted_chou12 Posted December 19, 2006 Share Posted December 19, 2006 for example "html.txt"I want the "html" part of the filename, does anybody know a easy way to achieve that?ThanksTed. Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/ Share on other sites More sharing options...
taith Posted December 19, 2006 Share Posted December 19, 2006 theres prolly an easier way... but this one has always worked wonders for me :-)[code]<?function get_filename($filepath,$extension=4){ $filepath = get_actualfilename($filepath); $length = strlen($filepath) - $extension; return substr($filepath,0,$length);}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144501 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 thanks. Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144508 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 but I think you need to define the function: get_actualfilename... as well, of whcih i dont have... Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144512 Share on other sites More sharing options...
taith Posted December 19, 2006 Share Posted December 19, 2006 sorry... here ya go...[code]<?function get_actualfilename($string){ if(stristr($string,'?')){ $pos = strpos($string, "?"); $string = substr($string, 0, $pos); } $string=basename($string); return $string;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144515 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 nope, doesnt work for me :'( Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144516 Share on other sites More sharing options...
taith Posted December 19, 2006 Share Posted December 19, 2006 you can cut out the whole get_actualfilename() part if your not intending on putting a full url in :) Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144518 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 okay, ill give it a try ;D Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144519 Share on other sites More sharing options...
papaface Posted December 19, 2006 Share Posted December 19, 2006 [code]list($name, $extension) = explode('.', $filename);[/code]$name = without extension$extension = extension Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144520 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 thanks. Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144524 Share on other sites More sharing options...
craygo Posted December 19, 2006 Share Posted December 19, 2006 can use this[code]<?phpfunction get_filename($filename){$file = substr($filename, 0, strpos($filename, strrchr($filename, ".")));return $file;}echo get_filename("johnny.inc.txt");?>[/code]this will get the file name no matter what. if you have a file johnny.html if will return johnnyif you have a file name johnny.inc.php it will return johnny.incRay Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144528 Share on other sites More sharing options...
ted_chou12 Posted December 19, 2006 Author Share Posted December 19, 2006 oh, i understand, because some filenames have more than one dot, is that right?Ted Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144546 Share on other sites More sharing options...
craygo Posted December 19, 2006 Share Posted December 19, 2006 yes and some file extension are more that 3 characters, not usually but html is one of them.Ray Link to comment https://forums.phpfreaks.com/topic/31240-getting-the-filename-of-the-file-divide-filename-away-from-file-extension/#findComment-144551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.