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

文字列「placeholder」を含むファイルをスキップするには:

if(strpos(file_get_contents($fname), 'placeholder') !== false) continue;
于 2013-10-28T20:46:34.183 に答える
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 に答える