file reader > echo html links (code inside)

Discussion in 'PHP' started by gluedup2, Jul 3, 2007.

  1. #1
    Basically, I have this code for reading directory contents:

    <?
    $mydir = "/path/to/dir/";
    $d = dir($mydir);
    while($entry = $d->read()) {
     if ($entry!= "." && $entry!= "..") {
     echo "<br><a href=\"/dir/",$entry,"\">$entry</a>";
     }
    }
    $d->close();
    ?>
    Code (markup):
    It reads the contents of the specified directory ($mydir) and prints a load of links to each of the files within that directory on the page it's placed on.

    My problem:
    As all filenames obviously have an extension, the link's anchor text ($entry) is appearing as the full filename like "mypage.php".

    How can I adapt the code to remove the file extention for the anchor text?

    Thanks in advance for any pointers and/or solutions.
     
    gluedup2, Jul 3, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    echo '<br><a href="/dir/'. $entry .'">'. preg_replace('/\.[a-z0-9]+$/i', null, $entry) .'</a>;
    
    PHP:
    Untested but should work.
     
    nico_swd, Jul 3, 2007 IP
    ansi likes this.
  3. gluedup2

    gluedup2 Peon

    Messages:
    101
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Nico, that particularly line came back with a syntax unexpected $end error.

    I changed it to the following and it now works superbly:
    echo "<br><a href=\"/dir/". $entry ."\">". preg_replace('/\.[a-z0-9]+$/i', null, $entry) ."</a>";
    Code (markup):
    Thanks for the help, wouldn't have got anywhere near it without you.
     
    gluedup2, Jul 3, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    My bad. :) Just forgot a quote at the end. Glad it works.
     
    nico_swd, Jul 3, 2007 IP