1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Removing the extension from a filename

Discussion in 'PHP' started by Colbyt, May 20, 2007.

  1. #1
    I don't seem to know the right search term so I can't find the answer to this one.

    I have a simple php script that retrieves a filename in the form of name.ext.

    When I echo that to the screen I wish to delete the .ext.

    How can you parse a variable to remove part of the string? In all cases it would of course be the part after the period but might vary from 3-4 characters.

    If parse is an incorrect term for this, correct me.
     
    Colbyt, May 20, 2007 IP
  2. redokai

    redokai Guest

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In PHP there's lots of functions that let you work with strings.

    For this particular example (assuming your file names have only one dot in them, separating filename and extension) you could just use:

    
    // let's assume $filename_with_extension = 'somefile.ext'
    list($filename, $extension) = explode('.', $filename_with_extension);
    
    echo $filename // outputs 'somefile'
    echo $extension // outputs 'ext'
    
    PHP:
    What explode() does is splitting a string into an array of strings. The splitting happens in this example everywhere where a dot occurs.
     
    redokai, May 20, 2007 IP
    Colbyt likes this.
  3. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hai try this:

    
     echo basename("name.ext",".ext")
    //this will return name only
    
    PHP:
     
    tamilsoft, May 20, 2007 IP
    Colbyt likes this.
  4. livingearth

    livingearth Well-Known Member

    Messages:
    1,469
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Great examples..
    I would have done something a lot more complex :)
     
    livingearth, May 20, 2007 IP
  5. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You have good answers already but here is one more way to do it, using basic string functions that you will want to learn about anyway:

    $str = "filename.jpg";
    $base = substr ($str, 0, strpos ($str, "."));
    echo "Filename: \"$str\"<br/>";
    echo "Basename: \"$base\"<br/>";
    
    PHP:
     
    lemaitre, May 20, 2007 IP
    Colbyt likes this.
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    If you have PHP 5.2.0 +, you can use pathinfo()

    
    $file = 'file.ext';
    $parts = pathinfo($file);
    
    echo $parts['filename']; // file
    
    PHP:
     
    nico_swd, May 20, 2007 IP
  7. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This will work with any PHP version:

    
    <?
    
    function extract_name($filename)
    {
    $pos=0;
    $pos2 = 0;
    $pos = strpos($filename,'.',0); 				// find the first occurance of .
    if ($pos===FALSE)
    { return $filename; 										// filename has no . so no extension, return $filename
    }
    while ($pos!==FALSE) 										// find the last occurance of . in the $filename
    {$pos2 = $pos;
     $pos = strpos($filename,'.',$pos+1);
    }
    if ($pos2==0) 
     return $filename;											// should never happen but just in case
    else
     return substr($filename,0,$pos2); 		// return everything before the last .
     
    }
    
    echo extract_name("filename.ext");
    
    ?>
    
    PHP:
    It works even with any file extension length by looking for the last dot in the string and returning what's on the left of that dot. If no extension found, the function returns the complete string.
     
    mariush, May 20, 2007 IP
    Colbyt likes this.
  8. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #8
    I used the method posted by redokai above because it looked like the easiest to implement into the existing script. It works but now I am getting duplicate entries for some but not all of the files in the directory.

    Except for the path statement at the top of the script, here is the entire script. Can anyone see what I did wrong?


    
    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    echo "Contents<br/>";
    
    //running the while loop
    while ($file = readdir($dir_handle)) 
    {
       if($file!="." && $file!="..") {$name=$file;
    	list($name, $extension) = explode('.', $name);
    }
       echo "<a href='uploads/$file'>$name</a><br/>";
    }
    
    //closing the directory
    closedir($dir_handle);
    PHP:
    Many thanks to all who have replied.
     
    Colbyt, May 20, 2007 IP
  9. redokai

    redokai Guest

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    What do you mean by duplicate entries? And where exactly in your code?
     
    redokai, May 20, 2007 IP
  10. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    According to php.net a readdir loop should look like this:

    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    echo "Contents<br/>";
    
    //running the while loop
    while (false !== ($file = readdir($dir_handle))) 
    {
      if($file!="." && $file!="..") 
      {
        $name=$file;
        list($name, $extension) = explode('.', $name);
        echo "<a href='uploads/$file'>$name</a><br/>";
      }
    }
    
    //closing the directory
    closedir($dir_handle);
    PHP:
    (and I also cleaned up what looked like a logic error).

    Not sure if that will fix duplicate entries, though. Maybe you have some files that are identical except for the extension?
     
    lemaitre, May 20, 2007 IP
  11. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #11
    $filename='file.txt';
    $files=explode('.'.$filename);
    echo $files[0];
     
    coderbari, May 21, 2007 IP
    Colbyt likes this.
  12. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #12
    coderbari, you should really reconsider using the function I've written.

    The method with explode works, no problem at all, but not in all cases.

    For example, If you have a file called archive.tar.gz do you really want the name to be archive or archive.tar ? Same thing for "Finance Document 100. Reviewed copy.doc" .... Returning the text on the left of the first dot is not really a good idea.
     
    mariush, May 21, 2007 IP
  13. Free Directory

    Free Directory Peon

    Messages:
    89
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    strrpos will deal with that one. For double extension files, that's compressed one. Don't know if they need that.
    the simple way,using regexp expresions:
    $name = preg_replace('/\.[^.]*$/', '', $FileName); 
    Code (markup):
     
    Free Directory, May 21, 2007 IP
  14. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #14
    yes mariush i agree with you.i never told your function is wrong :)
    it should be done using strpos() to get the last occurance of '.' and then removing the last portion of the filename
     
    coderbari, May 21, 2007 IP
  15. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #15

    Thanks a million!

    That did the trick. Not sure what you are calling the logic error but that must have been the problem with the duplicates.

    I notice you got the variable reset ($file=$name) on a line of it's own.

    Again many thanks to all.

    edit: the duplicate entries were with the file output. The first and fourth of 5 files were displayed in the output twice. As I said above the problem is solved.
     
    Colbyt, May 21, 2007 IP