How do I generate natural language index from filenames?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rbeschizza
Forum Newbie
Posts: 2
Joined: Wed Jun 30, 2004 11:54 am

How do I generate natural language index from filenames?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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

?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
rbeschizza
Forum Newbie
Posts: 2
Joined: Wed Jun 30, 2004 11:54 am

Post 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.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

feyd ,
your a virtual code god
Post Reply