blurredvision Posted June 13, 2008 Share Posted June 13, 2008 I'm using the scandir function to return an array of files with a directory, then I'm using a foreach to create a drop-down box with a list of those files. This works well and good, but now I only want to return .txt files to populate the drop-down. I'm not good with expressions (actually, I don't know how to do them at all), so I was hoping somebody could fill in the gap for me here. <?php $namefill = scandir($preconfdirectory, 0); foreach ($namefill as $value) { if ($value = expression here) { echo '<option value="' . $value . '">' . $value . '</option>'; } } ?> Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/110095-solved-how-to-only-use-txt-files-from-a-scandir-array/ Share on other sites More sharing options...
wildteen88 Posted June 13, 2008 Share Posted June 13, 2008 No need to use complex expressions just use substr foreach ($namefill as $file_name) { $ext = substr($file_name, -4); if(strtolower($ext) == '.txt') { echo '<option value="' . $file_name . '">' . $file_name . '</option>'; } } NOTE: My syntax for substr maybe wrong. I corrected it. Quote Link to comment https://forums.phpfreaks.com/topic/110095-solved-how-to-only-use-txt-files-from-a-scandir-array/#findComment-565014 Share on other sites More sharing options...
Barand Posted June 13, 2008 Share Posted June 13, 2008 As an alternative, you might want to look at the glob() function which will let you specify "*.txt" and create an array of just the .txt files Quote Link to comment https://forums.phpfreaks.com/topic/110095-solved-how-to-only-use-txt-files-from-a-scandir-array/#findComment-565115 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.