Jump to content

Strip file path


swamp

Recommended Posts

Hi there, i've got this code:

 

     
           <select name="poster" id="poster">
<?php
     foreach(glob('../../author/*.txt') as $file){ //loop through all .txt files
          $file = substr($file, 0, strrpos($file, '.')); //strip the .txt extension
          echo "<option value='{$file}'>{$file}</option>"; //print the option
     }
     ?> 
    </select> 

 

Which lists all the file names into the form drop down menu. I want to strip the file name at the moment its displaying '../../author/file_here' but I want it to just display 'file_here'

 

I'm sure this is easy enough to do, I've tried exploding the '/' but couldnt get my head round it really.

 

Any help appreciated!

 

Cheers

Link to comment
Share on other sites

There may be better methods to do this, probably regular expressions, but i'm giving you a working code with string manipulation.

 

<?php
foreach(glob('../../author/*.txt') as $file){
     $file = substr($file, strrpos($file, '/') + 1); //get the position of the last / and remove what's before it
     $file = substr($file, 0, strrpos($file, '.')); //get the position of the last dot and remove what's after it
     echo "<option value='{$file}'>{$file}</option>";
} 
?>

Link to comment
Share on other sites

you could simply use explode to accomplish this

 

$file = explode(".", basename($file));

 

this will now place your entire file name (with the extension) in an array

 

so to access just the extension you would call it like this:

 

echo $file[1];

 

for the filename like this

 

echo $file[0];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.