Page 1 of 1

How do I generate natural language index from filenames?

Posted: Wed Jun 30, 2004 11:54 am
by rbeschizza
I'm new to php, and this one is way beyond me.

We have archives of files and folders where the file and folder names use a formula, e.g.:

A folder called 281204 contains files from 28th December 2004

The file page1_28_12_04 is page 1 of a document from 28th december 2004

What I'd like to do is add something to my indexing PHP that takes the file or folder name, interprets it, and uses natural language in the link instead of the raw filename. i.e. the linking text for 281204 becomes "28 Dec 2004"

My guess is that you take the results of a readdir and explode each filename into arrays (renaming files if necessary to let explode work), then convert them to strings for month and year names and so on, which are echoed with the relevant link. But I have no idea how to actually do this. Any help actually accomplishing this would be much appreciated.

Posted: Wed Jun 30, 2004 12:28 pm
by feyd

Code: Select all

<?php

function rebuildDate($folderOrFilename)
{
	if(!preg_match('#(^\s*([0-9]{2})([0-9]{2})([0-9]{2}[0-9]{2}?))|(([0-9]{2})_([0-9]{2})_([0-9]{2}[0-9]{2}?)\s*$)#',$folderOrFilename,$matches))
		return FALSE;

	//print_r($matches);
	  
	if(isset($matches[5]))
	{	//	filename structuring
		$day = $matches[6];
		$mon = $matches[7];
		$year = $matches[8];
	}
	else
	{	//	folder structuring
		$day = $matches[2];
		$mon = $matches[3];
		$year = $matches[4];
	}
	
	return date("d M Y",mktime(0,0,0,$mon,$day,$year));
}

echo rebuildDate('281204')."\n";
echo rebuildDate('page1_28_12_04')."\n";

?>

Posted: Wed Jun 30, 2004 3:54 pm
by redmonkey
Untested but....

Code: Select all

<?php

function readable_date($string)
{
  $string = trim($string);
  if (preg_match('/^(?:page(\d+)_)?(\d{2})(_?)(\d{2})\\3(\d{2})$/', $string, $match))
  {
    $date = date("d M Y",mktime(0,0,0,$match[4],$match[2],$match[5]));
    return !empty($match[1]) ? 'Page ' . $match[1] . ' ' . $date : $date;
  }
  return false;
}

echo readable_date('281204')."\n";
echo readable_date('page1_28_12_04')."\n"; 
?>
Should output....

Code: Select all

28 Dec 2004
Page 1 28 Dec 2004

Posted: Thu Jul 01, 2004 12:09 pm
by rbeschizza
Thank you both for your replies. I wasn't able to get Feyd's working, however - it runs without error, but produces no output for me. unsure why.

Posted: Thu Jul 01, 2004 12:15 pm
by NewfieBilko
feyd ,
your a virtual code god