Announcement

Collapse
No announcement yet.

php to strip a file path

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    php to strip a file path

    I want to be able to strip down a path for an image in Actinic using php. (This is part of some other php so a function to do this would be ideal)

    If I have for example:
    \photos\image1.jpg then I want just image1.jpg returned.
    Also if if I have:
    \photos\folder\image1.jpg then I want just image1.jpg returned.
    And of course if the path is just:
    image1.jpg then it should return just image1.jpg.

    Basically everything to the left of the right most slash (and including the slashes) needs to be removed if existing.

    Can anyone help please?

    #2
    Originally posted by drounding View Post
    I want to be able to strip down a path for an image in Actinic using php. (This is part of some other php so a function to do this would be ideal)

    If I have for example:
    \photos\image1.jpg then I want just image1.jpg returned.
    Also if if I have:
    \photos\folder\image1.jpg then I want just image1.jpg returned.
    And of course if the path is just:
    image1.jpg then it should return just image1.jpg.

    Basically everything to the left of the right most slash (and including the slashes) needs to be removed if existing.

    Can anyone help please?
    This should meet the specification
    Code:
         function getfilename($filename)  {
            $namearray = explode ('\\', $filename) ;
            return array_pop($namearray);    
        }
    or you can simplify it further to
    Code:
         function getfilename($filename)  {
            return array_pop(explode ('\\', $filename));    
        }
    Explanation:
    The function 'explode' splits the file name into an array, splitting each item at the '\' character, the extra \ is needed as this is a special character in php.

    So if we pass \photos\folder\image1.jpg into the function we get an array with three items - photos, folder, and image1.jpg. If you have more folders to the file name it just means there will be more items in the array.

    The second function 'array_pop' removes the last item from the array (image1.php) and returns it as the result.

    The functions 'explode' and 'array_pop' are built into the php language.
    Last edited by malbro; 10-Jun-2011, 05:24 PM. Reason: added explanation

    Malcolm

    SellerDeck Accredited Partner,
    SellerDeck 2016 Extensions, and
    Custom Packages

    Comment


      #3
      Or if you like easy to understand code*:

      $FullFilename = "\photos\folder\image1.jpg" ;
      $MyFileName = strrchr($FullFileName,"\") ; Returns the string from last \
      $MyFileName= substr($MyFileName, 1) ; removes the inital \ from the result

      Mike

      * Or can't do complicated code (as I can't).
      -----------------------------------------

      First Tackle - Fly Fishing and Game Angling

      -----------------------------------------

      Comment


        #4
        Thanks guys. I'll them both a whirl.

        Comment


          #5
          Excellent, I have it working on the sites.
          Thanks again.

          Comment


            #6
            No coding needed. PHP has a basename function built in. And dirname for getting the path bit.
            Norman - www.drillpine.biz
            Edinburgh, U K / Bitez, Turkey

            Comment


              #7
              For interest.

              This was for a custom related products type functionality for use at section level that needed to be manually set as required.

              I used variables to hold the product id and them php to query the actinic database based on the product id to return the image filename and the product short description as well as to compose a link to the product.

              Comment


                #8
                No coding needed. PHP has a basename function built in. And dirname for getting the path bit.
                This is why it's good to have someone on the forum who knows what they're doing.

                Mike
                -----------------------------------------

                First Tackle - Fly Fishing and Game Angling

                -----------------------------------------

                Comment


                  #9
                  basename() works perfectly.
                  Thanks Norman.

                  Comment


                    #10
                    Nah! I just have a big link to the PHP manual on my desktop. I'm often surprised by what's already built-in.
                    Norman - www.drillpine.biz
                    Edinburgh, U K / Bitez, Turkey

                    Comment


                      #11
                      I want to use the image filename in a rich snippet, could i use this to produce the full url? Any suggests on exactly how would be great.

                      Comment

                      Working...
                      X