Get month from string

I have a bunch of files for our website that have the months in the file name. However, the months are in sometimes full name, sometimes 3 letter abbreviation.

Now, what I want to do is to rename the file name (I can do that) to a consistant naming convention. I can get the year out of the string ($year = preg_replace(“/[^0-9]/”, ‘’, $mfiles); where $mfiles is the file name), but cannot figure out the best way to get the 3 letter month from the file name.

I tried the following but get the year only.

$patterns[0] = "/Jan/";
$patterns[1] = "/Feb/";
$patterns[2] = "/Mar/";
$patterns[3] = "/Apr/";
$patterns[4] = "/May/";
$patterns[5] = "/Jun/";
$patterns[6] = "/Jul/";
$patterns[7] = "/Aug/";
$patterns[8] = "/Sep/";
$patterns[9] = "/Oct/";
$patterns[10] = "/Nov/";
$patterns[11] = "/Dec/";

$mfiles = "July_2012_Letter_Document.doc"

$year = preg_replace("/[^0-9]/", '', $mfiles); // ditch anything that is not a number
$month = preg_replace($patterns, '', $mfiles);
echo 'year: ' .$year . ' - month - ' . $month . ' - filename: ' . $mfiles . '<br>';

Any help is appreciated.


Are the file names really all in the same format?


Or, is there a range of challenges?


Ah, yes, more challenges than I want. But what you have is close. The year and month are there and the jist of the file type. Right now I just want to get the month out of the string. Oh, and sometimes the _ is there but more than not it is just a space.

In that case posting here a test array containing one of each of the possible formats is probably the only way you are going to get a robust and definitive solution on here.

the trick with the three letter shorthands are that you cant guarantee their meaning.

Jans_Spreadsheet_March. What month is that file?

Now, if they will always put the date first, this makes it a bit easier.
(completely untested and probably simplifyable)

$months = array("Jan","Feb"....."January","February"....."December");
$str = str_replace("_"," ",$str); //Make all files space-delimited.
$str = explode(" ",$str);
if(intval($str[0]) == 0 && $str[0] != "00") {
 //A Month Came First.
 $m_ind = 0;
 $y_ind = 1;
} else {
 $y_ind = 0;
 $m_ind = 1;
 $file_month = (array_search($str[$m_ind],$months) !== FALSE) ? (array_search($str[$m_ind],$months) % 12) + 1 : FALSE;
 $file_year = (strlen($str[$y_ind]) == 4) ? $str[$y_ind] : (($str[$y_ind] < 20) ? "20".$str[$y_ind] : "19".$str[$y_ind]);

$file_month will be an integer in the range 1-12 or FALSE if something other than a month string was detected.
$file_month will be a year in 4-digit format, with a range for 2-to-4 digit conversion of 1921-2020.