decoding Timestamps explicitly encoded in a filename

Quite a while ago I decided to change the way filenames are formatted when users upload a file to my website(which was a great idea!) to include the UNIX_Timestamp in the filename, this proved very useful after unfortunately the timestamps associated with files in my database was wiped, the filename structure has changed considerably and I am currently trying to recover the valid timestamp from the filename, however so far I have only been able to recover half.

Beforehand, I structured the filename to be like this:

dcd1322318879.png - Letters prepended by the unix timestamp.

Then, I allowed people to start logging in and made the filename much more Human Friendly, they now read as follows;

username-09092012-183422.jpeg where username is the logged in users name. The problem I am having now is the fact that firstly, I am awful with regex, for the previous filenames(without the user data and human friendly date) my code was:

$newDate = preg_replace("/[^0-9]/","", $data['name']);

However obviously that will not work now as usernames can contain numbers, and therefore that number will also be included in the final number for the date.

I am currently wondering if there is anyway I can resolve this issue, I guess the fact I added hyphens(-) will be useful as I can use it as a separator, however I have no idea where to start. I need to just get the last two parts of the filename, which contain the date & time so I can then convert them into unix timestamps.

Any help is greatly appreciated


There you go, yet another way, including translation to unix timestamp:

preg_match_all('/[0-9]+/', $string, $matches);
$d =  DateTime::createFromFormat('dmY His', $matches[0][0].' '.$matches[0][1]);
$timestamp = $d->getTimestamp();

Try this:

preg_match_all("/(\d{8})-(\d+)/", $str, $matches);

the most easiest way is using

  $array = explode ("-", $filename); 
  print_r ($array);

unless you really want to go with regex, but I dont think its needed since the system is the one doing all the creating. Meaning there will be no user inputs... it can also depends on how you have your script setup.

From your question, I assume that the hyphens can be used as the structure for the regex. That is, usernames and dates won't contain them.

So then, this pretty simple pattern would work:


Group 1 will contain the username, group 2 contains the date, and group 3 contains the filename (note that the filename (everything after the third hyphen) can be anything at all, including more hyphens).

(the .*? is the dot, quantified at 'zero or more times' by the *, and the ? makes the quantifier lazy, so that the . doesn't just greedily race to the end of the string)

Need Your Help

PERL: parsing a multiple line result

perl parsing

I was looking for the best way to parse some text results which appear on multiple line.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.