swamp Posted August 20, 2008 Share Posted August 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
ahowell Posted August 20, 2008 Share Posted August 20, 2008 <?php foreach (glob('../../author/*.txt') as $file){ $fileParts = pathinfo($file); echo "<option value='{$fileParts['filename']}'>{$fileParts['filename']}</option>"; } Quote Link to comment Share on other sites More sharing options...
trq Posted August 20, 2008 Share Posted August 20, 2008 <?php foreach (glob('../../author/*.txt') as $file){ $file = str_replace('.txt','',basename($file)); echo "<option value='$file'>$file</option>"; } ?> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Share Posted August 20, 2008 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]; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.