Reputation: 3442
I would like to archive all log files of the month at the end of this month. for that I use the following code :
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set('Europe/Berlin');
function archive_file($overwrite)
{
//$log_file_name = date('Y-m-d',strtotime('-1 days')).'.log';
$log_file_name = date('Y_m_d').'.log';
$zip_file = date('Y_m').'_Update_log.zip';
if(file_exists($zip_file) && !$overwrite)
return false;
$zip = new ZipArchive();
if($zip->open('E:\\'.$zip_file,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)
return false;
$package_dir = 'E:\log';
$files = scandir($package_dir);
foreach ($files as $file_name)
{
echo "\n *******\n";
echo $file_name;
echo "****************\n";
//log file format = Y-m-d.log
//if(file format = format) {
$zip->addFile('E:\\'.$log_file_name, $log_file_name);
//}
}
$zip->close();
}
echo "*****************************Archive files***************\n";
archive_file(false);
?>
I wonder if I can use regular expression to check log file format Y-m-d.log (2015-01-07.log) to archive only all log files of one month (example january ).
Any help will be appreciated
Upvotes: 0
Views: 72
Reputation: 831
try this it will match your logic :
preg_match('/'.date('Y_m').'_\d.*/', $file_name)
Upvotes: 1
Reputation: 8189
Try this :
$regex = '/[0-9]{4}\-(.*?)\-/';
$matches = null;
preg_match($regex, $file_name, $matches);
if(isset($matches[1]) && $matches[1] == '01') {
echo $file_name.' file is a log of january';
}
Or if you don't need to get the matches :
if(preg_match('/[0-9]{4}\-01\-/', $file_name)) {
echo $file_name.' file is a log of january';
}
Upvotes: 1