How to remove part of a string?

Discussion in 'PHP' started by Stefany93, Sep 6, 2012.

  1. #1
    Hello friends,

    I have a file upload system where users can upload file and I store the files on the separate folder but I upload the files' names in the mysql DB. Now what I want to do is to remove all the letter after the dot sign i.e the extension and only upload the name of the file without the extension name.
    For example if the file's name is syllabus.zip I want to remove 'zip' and be left alone with only 'syllabus'.

    Could you please tell me how to do that?

    Thank you!

    Best Regards
    Stefany
     
    Solved! View solution.
    Stefany93, Sep 6, 2012 IP
  2. #2
    Found something via google that might help you out!

    From stackoverflow
    http://stackoverflow.com/questions/2395882/how-remove-extension-from-string-only-real-extension
    Code (markup):
    Hope that helps!
     
    GMF, Sep 6, 2012 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I would probably do something like this.

    $original = 'syllabus.zip';
    
    $file_name = substr($original,0,strpos($original,'.'));
    PHP:
    This will remove everything after and including the period. I don't think there is a more efficient way of doing this.
     
    jestep, Sep 6, 2012 IP
  4. Stefany93

    Stefany93 Greenhorn

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Thank you very much guys, you are amazing!
     
    Stefany93, Sep 6, 2012 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    deathshadow, Sep 6, 2012 IP
  6. Stefany93

    Stefany93 Greenhorn

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    ^^ Thank you very much deathshadow, much appreciated!
     
    Stefany93, Sep 7, 2012 IP
  7. greyinfotech

    greyinfotech Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    do not use strpos

    how about grey.abc.txt

    strpos will return first occurence of dot and the part of the file name you get will be 'grey' ( but it should be grey.abc as txt is only extension)

    use strrpos instead . strrpos returns last occurence of the period.
     
    greyinfotech, Sep 18, 2012 IP