SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract filname from the url

    Hei,

    In example I have this different types of url's:
    - 'http://www.mydomain.no/user/folder1/site1.php'
    - 'http://www.mydomain.no/user/folder2/site2.php'
    - 'http://www.mydomain.no/user/folder1/folder3/site3.php'
    - 'http://www.mydomain.no/user/folder5/site4.php?s=34&h=fast'

    Question: How can get the name of the file (site1, site2, site3....) in all this differnt sorts of url?
    If it's possible I would also like to NOT have the extension with the filname.
    (This part is always the same: 'http://www.mydomain.no/user/')

    This is what I have so far (Works partly for the three first examples. Getting the extensions aswell)
    PHP Code:
    $fileName end(explode("/"$REQUEST_URI)); 
    Can someone please put me in the right direction on this.

    -Helge

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is $SCRIPT_NAME what you need?

    S

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I can see $SCRIPT_NAME and $REQUEST_URI return the path to the file and the filname. I need to, in some way, only to have the filname in the uri.

    Sorry if explained myself bad in the first post.

    -Helge

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    W scoates said was replace SCRIPT_NAME into your existing code, as REQUEST_URI returns the query string as well.

    PHP Code:
    $fileName end(explode("/"$SCRIPT_NAME)); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use ]basename() to get the filename (with extensions)... followed by a regexp replace function to remove the extensions (i'm not good at this so i'll leave that part to the experts)
    PHP Code:
    basename($SCRIPT_NAME); 

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I din't understood that.

    Anyway, Thanks Scoates and freddydoesphp for putting me on the right track !
    This is what I ended up with:
    PHP Code:
    $fileName str_replace(".php3"""end(explode("/"$SCRIPT_NAME))); 
    -Helge


    PS!
    Redemtion,
    At first I found your suggestion to be the easiest. I tried this code
    PHP Code:
    $fileName basename($SCRIPT_NAME".php3");
    // If the filename ends in suffix this will be cut off 
    But then I found a comment in the manual saying that it could interrupt with pre-defined variables like $PHP_SELF, and I don't think I would risk that. Please correct me if I'm wrong.

  7. #7
    SitePoint Addict AbelaJohnB's Avatar
    Join Date
    Oct 2001
    Location
    Eureka, CA. USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you give us an example of a url you have.... and an example of what you want to end-up-with






    Later.
    John B. Abela
    www.JohnAbela.Com

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for still looking into my problem AbelaJohnB.

    But I've actually solved my problem, as I said in my prev. post (maybe I did not explained to good )

    This is the code I ended up with
    PHP Code:
    $fileName str_replace(".php3"""end(explode("/"$SCRIPT_NAME))); 
    At the end of my prev post I was just trying to say that at first glance redemption's suggestion seemed nice, but after reading the manual I changed my opinion.

    Hope this clearified.

    -Helge

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Helge

    But then I found a comment in the manual saying that it could interrupt with pre-defined variables like $PHP_SELF, and I don't think I would risk that. Please correct me if I'm wrong.
    i don't see how it could interupt with any pre-defined variables because in effect i'm only reading in the variable $SCRIPT_NAME... i'm not 'writing' anything ... where did you see that comment?

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the fourth comment om this site in the manual: http://www.php.net/basename.

    PHP Code:
    //basename() corrupts the first input when the optional second parameter is used.

    //This effects global and pre-defined variables, so be careful 
    //(E.g. if you use $PHP_SELF - it will be modified for the lifetime of the script).

    //To avoid this, place the first paramter in quotes:

    $file "/path/to/file/index.html";
    basename($file,".html");
    print 
    $file;
    // output is: /path/to/file/indexhtml

    $file "/path/to/file/index.html";
    basename("$file",".html");
    print 
    $file;
    // output is: /path/to/file/index.html 
    It might well be that I misunderstand what the comment is saying...

    -Helge


Bookmarks

Posting Permissions

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