0

我这里有一段代码,它从目录中获取最后修改的文件。但是,我希望它排除任何上面写有“占位符”一词的文件。我该如何修改它以排除这些文件,特别是因为在其页面上写有“占位符”的文件通常是最后修改的文件?

这是我拥有的代码:

<?php
$dir = "path goes here";         
$pattern = '\.(php)$'; // check only file with these ext.          
$newstamp = 0;            
$newname = "";

if ($handle = opendir($dir)) {               
       while (false !== ($fname = readdir($handle)))  {            
         // Eliminate current directory, parent directory            
         if (ereg('^\.{1,2}$',$fname)) continue;            
         // Eliminate other pages not in pattern            
         if (! ereg($pattern,$fname)) continue;            
         $timedat = filemtime("$dir/$fname");            
         if ($timedat > $newstamp) {
            $newstamp = $timedat;
            $newname = $fname;
          }
         }
        }
closedir ($handle);
$name = basename($newname,".php");
 echo "<b><span class='latestch'>Latest Chapter:</span></b>  <a href=url-path-goes-here".$newname."> $name</a>";
?>
4

2 回答 2

0

你可以节省很多代码:

array_multisort(
    array_map('filemtime', 
        $files = preg_grep('/placeholder/',
            glob("$dir/*.php"), PREG_GREP_INVERT)),
    SORT_ASC, $files);

$name = basename(end($files), '.php');
于 2013-10-28T21:14:52.507 回答
0

跳过包含字符串“placeholder”的文件:

if(strpos(file_get_contents($fname), 'placeholder') !== false) continue;
于 2013-10-28T20:46:34.183 回答