0

I have a php script that works great, but I want to echo a heading that lists the month it is for. The file names will have either a _01, _02, _03 etc. in them. I've created an array of $months, but I'm trying to figure out what the best way is to do this.

If file name contains _01, echo January. Else if file name contains _02, echo February. Anyone know of the best practice for this scenario?

foreach (glob("*.mov") as $filename)

$theData = file_get_contents($filename) or die("Unable to retrieve file data");

$months = ['_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09', '_10', '_11', '_12'];
ValleyDigital
  • 1,460
  • 4
  • 21
  • 37
  • You're probably looking for a endswith() function: http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions – tyteen4a03 May 31 '13 at 21:16

4 Answers4

1

maybe something similar to this - based on fact that month number will be in filename only once

foreach($theData as $filename){
    preg_match('/_(\d{2})/', $filename, $match);
    echo date('F',strtotime($match[1].'/20/2000'));
}
Ochi
  • 1,468
  • 12
  • 18
1

Try this:

foreach (glob("*.mov") as $filename)

$theData = file_get_contents($filename) or die("Unable to retrieve file data");

$months = ['January' => '_01', 'February' =>  '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12'];

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){

      echo $key;

  }
}
Mieszko
  • 56
  • 1
0

Here is what I was able to come up with

$files = [
    "file_01",
    "file_02asdf",
    "file_03_sfsa",
    "file_04_23",
    "file_05 cat"
];

foreach($files as $file){
    preg_match("/_(\d{2,2})/", $file, $matches);
    echo date("F", strtotime("2000-{$matches[1]}-01"))."\n";
}

which results in:

January
February
March
April
May

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

maybe thats another way, but preg_match seems to be smarter

function getMonthnameByFilename($filename,$months) {
   foreach($months as $index => $m) {
        if(strpos($filename,$m) !== false) {
          return date("F",strtotime("2013-".($index+1)."-1"));
        }
    }
    return false;
}
steven
  • 4,868
  • 2
  • 28
  • 58