开发者

Sort files by filenames in PHP - March 2011, April 2011, May 2011 etc

开发者 https://www.devze.com 2023-02-17 05:56 出处:网络
I have a directory of files with the following file names: August 2008 Presentation.ppt August 2009 Presentation.pdf

I have a directory of files with the following file names:

  • August 2008 Presentation.ppt
  • August 2009 Presentation.pdf
  • August 2010 Presentation .pdf
  • February 2008 Presentation.ppt
  • January 2011 Presentation.pdf
  • March 2010 Presentation.pdf
  • March 2011 Presentation.pdf
  • March 2007 Presentation.ppt
  • March 2009 Presentation.ppt
  • November 2006 Presentation.pdf
  • October 2009 Presentation.ppt

I am trying to sort them so that they appear in this manner: Presentation March 2011

  • Presentation January 2011
  • Presentation August 2010
  • Presentation March 2010
  • Presentation October 2009
  • Presentation August 2009
  • Presentation March 2009
  • Presentation August 2008
  • Presentation March 2007
  • Presentation November 2006

I am using this code so far:

$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>";

Is it even possible to sort the files in the way that I want them? All the code I try to use just brings back the list on alphabetical order.

If it isn't possible to do this, can anyone suggest a way to rename my files and the code to sort them?

Any help is much appreciated.


You can try using a usort (uses a function to compare the values). Then, in your function, convert the filename to a timestamp, using preg_match to take the date portion out of your filename, and then strtotime to convert it to a timestamp that can be compared:

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');

Note: this function sorts in descending order, so you don't have to do array_reverse.


Write a custom comparison function for usort. That comparison function would extract the name of the month from the filenames, convert it to an integer using

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

and compare the integers.


I would just rename the files to yyyymm.xxx (August 2008 Presentation.ppt -> 200808.ppt, January 2011 Presentation.pdf -> 201101.pd etc ) format. Then use the following code (only change is the addition of months array, the print statement and the sort method).

$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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号