Jonathan
Jonathan

Reputation: 129

Correct preg_match code to sort filenames by date?

I have a function that takes the name of files from a folder, sorts them by date and then creates a link to the file. However, this only works if the actual filename has spaces between the words. If I add hyphens into the filename, the order by date doesn't work correctly.

The filenames that work are:

Presentation January 2011.pdf

Presentation August 2010.pdf

Presentation May 2010.pdf

If I add hyphens to the filenames the order breaks:

Presentation-January-2011.pdf

Presentation-August-2010.pdf

Presentation-May-2010.pdf

How can I alter the preg_match() so that it takes into account hyphens? Here is my code:

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

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

foreach ($files as $file){
    $name = substr($file, 0, strrpos($file, '.'));
    $filename = str_replace(" ", "%20", $file);
    $name = str_replace("-", " ", $file);
    print "<li><a href='/$linkdir/$filename' rel='external'>$name</a></li>";
}

Any help on this would be much appreciated.

Upvotes: 0

Views: 484

Answers (3)

Jim
Jim

Reputation: 1

Then what happens when a different character is used, you're constantly updating your code. Why not use something like /\w+\W?\d{4}/ to capture any non-alphanumeric character that MIGHT show up?

Upvotes: 0

RickN
RickN

Reputation: 13500

The following two lines:

preg_match('/\w+ \d{4}/', $a, $matches_a);
preg_match('/\w+ \d{4}/', $b, $matches_b);

They match a number of 'word-like characters' (\w), a space and then four digits (\d).

You could either change the regular expression to accept a space or a dash: '[ -]' or '( |-)' instead of the space ''. This should not break the strtotime() function calls.

If it does, you could change date_sort_desc() by adding the following at the top:

$a = str_replace("-", " ", $a);
$b = str_replace("-", " ", $b);

In that case, you wouldn't need to change the regular expressions.

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

'/\w+ \d{4}/' looks for a word, a blank and four digits; '/\w+[ -]\d{4}/' should look for a blank or a hyphen between the word and the digits.

Upvotes: 1

Related Questions