greenlightning Posted June 30, 2010 Share Posted June 30, 2010 So I found this script that lists files in a directory: <?php if ($handle = opendir('pdf')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="pdf/'.$file.'">'.$file.'</a> <br> <br>'; } } closedir($handle); } ?> <P>List of files:</p> <P><?=$thelist?></p> My php knowledge is extremely limited and I want to figure out how to modify this script so that it'll only display files that contain a pattern. Example: Files begin with "ars". Help me out here please. Link to comment https://forums.phpfreaks.com/topic/206295-list-files-that-contain-a-text-pattern/ Share on other sites More sharing options...
dimitar Posted June 30, 2010 Share Posted June 30, 2010 <?php if ($handle = opendir('pdf')) { $thelist = ''; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && preg_match("/\bars/i", $file)) { $thelist .= '<a href="pdf/'.$file.'">'.$file.'</a> <br> <br>'; } } closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/206295-list-files-that-contain-a-text-pattern/#findComment-1079222 Share on other sites More sharing options...
kenrbnsn Posted June 30, 2010 Share Posted June 30, 2010 I would use the glob function: <?php $files = glob('pdf/ars*'); $tmp = array(); foreach ($files as $file) { $tmp[] = '<a href="$file">' . basename($file) . '</a> <br> <br>'; } echo implode("\n",$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/206295-list-files-that-contain-a-text-pattern/#findComment-1079226 Share on other sites More sharing options...
greenlightning Posted June 30, 2010 Author Share Posted June 30, 2010 sweet, thanks. Link to comment https://forums.phpfreaks.com/topic/206295-list-files-that-contain-a-text-pattern/#findComment-1079237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.