Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    printing the filename with out extension

    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.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php
    $file 
    "welcome.txt";
    $aFile explode("."$file);
    $filename $aFile[0];
    $ext $aFile[1];
    echo 
    $filename .'<br />'.$ext;
    ?>

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
    $file "/path/to/file.txt";

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

  • #4
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi,

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


    thanks
    mrjameer.

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts

  • #6
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do you like that one:


    PHP Code:
    <?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)
    ?>
    Attached Files Attached Files
    Last edited by fackelkind; 03-22-2007 at 02:30 AM. Reason: Update

  • #7
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    using nightfires method would be something like this
    PHP Code:
    <?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;
    }
     
    ?>

  • #9
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Did you look at the link that ralph|mayo gave you?
    PHP Code:
    $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' 

  • #10
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah indeed, just pathinfo()
    why are so many people messing with regexps and stuff????
    pathinfo works just fine!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •