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.

Help with Glob () output

Discussion in 'PHP' started by Colbyt, Aug 14, 2007.

  1. #1
    I just discovered glob as a faster way to perform a task I need done. I want to read a subdirectory and create links with the output. Here is the code and the questions follow.

    <?php
    //Take all file names in the directory and put them in a link.
    
    foreach (glob("dir_name/*.*") as $filename) 
    {
       echo "<a href=\"".$filename."\">".$filename."</a><br/>";
    }
    ?> 
    
    Code (markup):
    This works fine except the output to the browser reads as dir_name/filename.ext.

    What I want to do is maintain the link exactly as it is and strip the dir_name and .ext (dot extension) from the browser display. I fell like I need to use explode but am not sure exactly to write the code to do that.
     
    Colbyt, Aug 14, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ecentricNick, Aug 14, 2007 IP
    Colbyt likes this.
  3. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    
    foreach (glob("dir_name/*.*") as $filename) 
    {
       echo '<a href="' . $filename. '">' . array_pop(explode('/', array_shift(explode('.', $filename)))) . '</a><br/>';
    }
    
    PHP:
    There you go, although strictly speaking, you shouldn't have a function inside a function, let alone 3 functions inside 1. But as you're attempting to do it all in 1 line, I'm sure you'll appreciate it more this way.
     
    Wildhoney, Aug 14, 2007 IP
    Colbyt likes this.
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    For PHP 5.2.0+
    
    pathfinfo($filename, PATHINFO_FILENAME)
    
    PHP:
    Or:
    
    basename($filename, '.' . end(explode('.', $filename)))
    
    PHP:

    Umm... before I rant about it, I'd rather hear your reason for this. :)
     
    nico_swd, Aug 14, 2007 IP
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    His posted solution worked fine. I don't know enough to comment on his statement. I can say that the execution slowed down based on the straight glob. Just enough to notice. Most people would not.
     
    Colbyt, Aug 14, 2007 IP
  6. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    I have no personal reasons as to why not aside from readability, but if you put PHP in its pedantic mode it will throw you a notification.

    Reps...?
     
    Wildhoney, Aug 14, 2007 IP