Jump to content

List Files That Contain a Text Pattern


greenlightning

Recommended Posts

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

<?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);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.