首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按PHP文件名对文件进行排序-2011年3月、2011年4月、2011年5月等

按PHP文件名对文件进行排序-2011年3月、2011年4月、2011年5月等
EN

Stack Overflow用户
提问于 2011-03-17 14:54:13
回答 3查看 1.8K关注 0票数 2

我有一个文件目录,文件名如下:

  • 2008年8月Presentation.ppt
  • 2009年8月Presentation.pdf
  • 2010年8月介绍.pdf
  • 2008年2月Presentation.ppt
  • 2011年1月Presentation.pdf
  • 2010年3月Presentation.pdf
  • 2011年3月Presentation.pdf
  • 2007年3月Presentation.ppt
  • 2009年3月Presentation.ppt
  • 2006年11月Presentation.pdf
  • 2009年10月Presentation.ppt

我正试图对它们进行排序,使它们以这种方式出现:2011年3月的演示

  • 2011年1月
  • 2010年8月
  • 2010年3月介绍
  • 2009年10月
  • 2009年8月
  • 2009年3月介绍
  • 2008年8月
  • 2007年3月介绍
  • 2006年11月

到目前为止,我正在使用以下代码:

代码语言:javascript
复制
$linkdir="documents/presentations";
$dir=opendir("documents/presentations");
$files=array();

while (($file=readdir($dir)) !== false)
{
   if ($file != "." and $file != ".." and $file != "index.php")
   {
    array_push($files, $file);
   }
}

closedir($dir);

natcasesort($files);

$files=array_reverse($files);

foreach ($files as $file)
print "<li><a href='/$linkdir/$file' rel='external'>$file</a></li>";

有可能按照我想要的方式对文件进行排序吗?我尝试使用的所有代码都按字母顺序返回列表。

如果这是不可能做到的,有人能建议一种方法来重命名我的文件和代码排序吗?

任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-17 15:04:50

您可以尝试使用usort (使用函数来比较值)。然后,在函数中,将文件名转换为时间戳,使用preg_match从文件名中提取日期部分,然后使用strtotime将其转换为可比较的时间戳:

代码语言:javascript
复制
function date_sort_desc($a, $b)
{
  preg_match('/\w+ \d{4}/', $a, $matches_a);
  preg_match('/\w+ \d{4}/', $b, $matches_b);
  $timestamp_a = strtotime($matches_a[0]);
  $timestamp_b = strtotime($matches_b[0]);
  if ($timestamp_a == $timestamp_b) return 0;
  return $timestamp_a < $timestamp_b;
}

usort($files, 'date_sort_desc');

注意:此函数按降序排序,因此不必执行array_reverse

票数 3
EN

Stack Overflow用户

发布于 2011-03-17 15:00:23

乌斯托编写自定义比较函数。该比较函数将从文件名中提取月份名称,然后使用以下方法将其转换为整数

代码语言:javascript
复制
array(
  'January' => 0,
  'February' => 1,
  'March' => 2,
  'April' => 3,
  'May' => 4,
  'June' => 5,
  'July' => 6,
  'August' => 7,
  'September' => 8,
  'October' => 9,
  'November' => 10,
  'December' => 11
);

比较整数。

票数 1
EN

Stack Overflow用户

发布于 2011-03-17 15:25:04

我将文件重命名为yyyymm.xxx (2008年8月Presentation.ppt -> 200808.ppt,2011年1月Presentation.pdf -> 201101.pd等)格式。然后使用以下代码(仅更改添加月份数组、print语句和排序方法)。

代码语言:javascript
复制
$linkdir="documents/presentations";
$dir=opendir("documents/presentations");
$files=array();

while (($file=readdir($dir)) !== false)
{
   if ($file != "." and $file != ".." and $file != "index.php")
   {
    array_push($files, $file);
   }
}

closedir($dir);

sort($files);

$files=array_reverse($files);
$months = array("","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 


foreach ($files as $file)
print "<li><a href='/$linkdir/$file' rel='external'>Presentation " . $months[(int)substr($file,3,2)] . " " . substr($file,0,4) . "</a></li>";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5340765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档