SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.
    Code:
    $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.

    E

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are the file names really all in the same format?

    month_year_Letter_Document.doc
    mon_year_Letter_Document.doc

    Or, is there a range of challenges?

    year_month_Letter_Document.doc
    yy-mon_Letter_Document.doc

  3. #3
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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)
    PHP Code:
    $months = array("Jan","Feb"....."January","February"....."December");
    $str str_replace("_"," ",$str); //Make all files space-delimited.
    $str explode(" ",$str);
    if(
    intval($str[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) + 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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •