我需要我的脚本按日期对.txt文件进行排序。 它就像一个简单的新闻脚本,我所做的是添加名为:
的.txt文件[23.7.13]新酷标题 [24.7.13]建议和提示
并回复内容, 已准备好一切,包括回声部分 但它不会在第一次约会时对它们进行排序..我怎么能这样做?
<?
if( $handle = opendir( 'includes/news' ))
{
while( $file = readdir( $handle ))
{
if( strstr( $file, "txt" ) )
{
$addr = strtr($file, array('.txt' => ''));
echo '<h1><a href="?module=news&read=' . $addr . '">»' .
$addr . "</a></h1>";
}
}
closedir($handle);
}
}
?>
答案 0 :(得分:0)
$files = array();
$dir = new DirectoryIterator('includes/news');
foreach ($dir as $fileinfo) {
$files[$fileinfo->getMTime()] = $fileinfo->getFilename();
}
ksort($files);
编辑:(试试这个);
<?
$dir = new DirectoryIterator('includes/news');
//$dir = new DirectoryIterator('.');
$num = 0;
foreach ($dir as $fileinfo) {
$name = $fileinfo->getFilename();
preg_match_all('/\[(.*?)\]/',$string, $matches);
//preg_match_all('/\-(.*?)\-/',$name, $matches);
if(!empty($matches[1])) {
//create timestamp (to use for key)
$timestamp = strtotime($matches[1][0]);
//use this as array key (to order by with ksort()
$files[$timestamp]['name'] = $name;
}
}
//sort
ksort($files);
//get the key back to the normal date
foreach($files as $key=>$value) {
$sorted_files[date('d.m.Y',$key)] = $value;
}
var_dump($files);
?>