Last active
December 26, 2015 05:19
-
-
Save stevenrombauts/7099609 to your computer and use it in GitHub Desktop.
Try to extract the date from filenames by guessing with different formats.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Run over different date patterns | |
* and attempt to extract the correct date from a given string. | |
* | |
* @param String containing date. | |
* @return date in the YYYY-MM-DD format, or FALSE if no match was found. | |
**/ | |
function grab_date($str) | |
{ | |
$months_short = array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'); | |
$months_long = array('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'); | |
$patterns = array( | |
'/([0-9]{2})[\-\.\_]([0-9]{2})[\-\.\_]([0-9]{4})/' => array(3,2,1), | |
'/([0-9]{4})[\-\.\_]([0-9]{2})[\-\.\_]([0-9]{2})/' => array(1,2,3), | |
'/([0-9]{1,2})[\-\.\_ ]?([a-zA-Z]{3,9})[\-\.\_ ]?([0-9]{4})/' => array(3,2,1) | |
); | |
foreach($patterns as $pattern => $indices) | |
{ | |
if(preg_match($pattern, $str, $matches)) | |
{ | |
$parts = array(); | |
foreach($indices as $index) { | |
$parts[] = $matches[$index]; | |
} | |
// Deal with non-numeric months | |
if(!is_numeric($parts[1])) | |
{ | |
$haystack = strlen($parts[1]) == 3 ? $months_short : $months_long; | |
$search = array_search(strtoupper($parts[1]), $haystack); | |
if($search !== false) { | |
$parts[1] = $search + 1; | |
} | |
} | |
// Zero-pad month/day | |
for($i=1;$i<3;$i++) { | |
$parts[$i] = sprintf("%02d", $parts[$i]); | |
} | |
return implode('-', $parts); | |
} | |
} | |
return false; | |
} | |
// Example usage: | |
$filenames = explode("\n", file_get_contents('example.txt')); | |
foreach($filenames as $filename) | |
{ | |
if($filename == '') { | |
continue; | |
} | |
$date = grab_date($filename); | |
if($date !== false) { | |
echo $filename . ' contains date: ' . $date . PHP_EOL; | |
} else { | |
echo 'Could not find a date in ' . $filename . PHP_EOL; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
01-Markus_Schulz_-_Global_DJ_Broadcast_(DI.fm)-NET-2008-04-10-PS.mp3 | |
01-Markus_Schulz_-_Global_DJ_Broadcast_(DI.fm)-NET-2008-08-28-PS.mp3 | |
01-Markus_Schulz_-_Global_DJ_Broadcast_(DI.fm)-NET-22-02-2007-PS.mp3 | |
01-Markus_Schulz_-_Global_DJ_Broadcast_-_ISS_-_guest_Jody_Wisternoff-NET-11-09-2008.mp3 | |
01-markus_schulz-global_dj_broadcast_(the_mix)_(party_931)-cable-01-10-2005-mim.mp3 | |
01-markus_schulz-global_dj_broadcast_(winter_music_conference)_(party_931)-cable-03-10-2004-mim.mp3 | |
01-markus_schulz_-_global_dj_broadcast__world_tour_san_francisco-sbd-03-04-2010.mp3 | |
01_markus_schulz_-_gdjb_ibiza_summer_sessions__incl_jody_wisternoff_guestmix-07-26-2007.mp3 | |
01_markus_schulz_-_global_dj_broadcast_2006-11-30_(di.dm)-tt.mp3 | |
Markus Schulz - Global DJ Broadcast (22-05-2008).mp3 | |
Markus Schulz presents - Global DJ Broadcast 2010.04.08.mp3 | |
Markus Schulz presents - Global DJ Broadcast World Tour (7 August 2008) - DJM.mp3 | |
Markus_Schulz-Global_DJ_Broadcast_(2010-02-04).mp3 | |
Markus_Schulz-Global_DJ_Broadcast_Cosmic_Gate_Guestmix-21-12-2006-TRANCEZONE.mp3 | |
Markus_Schulz_-_Global_DJ_Broadcast_(10October2007)_URTranceZone.com.mp3 | |
Markus_Schulz_-_Global_DJ_Broadcast_-_Guest_Matt_Darey-NET-26-06-2008.mp3 | |
Markus_Schulz_-_Global_DJ_Broadcast__24JAN2008__URTranceZone.com.mp3 | |
Markus_Schulz_-_Global_DJ_Broadcast__28FEB2008__URTranceZone.com.mp3 | |
Markus_Schulz___Global_DJ_Broadcast__13_December_2007_www.trancezone.tv.pl.mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_(18_December_2008).mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_(18_September_2008).mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_(27_September_2007) HOUR I uploaded by BV.mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_(28_May_2009).mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_-_06.05.2010.mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast_Ibiza_Summer_Sessions_(10_September_2009).mp3 | |
Markus_Schulz_presents_-_Global_DJ_Broadcast__23_April_2009_.mp3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment