Description:
I have a directory that contains a large number of files named as follows:
2013_11_weekly.csv
2013_10_weekly.csv
2013_08_weekly.csv
2013_09_weekly.csv
2013_11_any_string.csv
2013_10_any_string.csv
Problem:
How can I iterate over the directory and merge all the files that end with the same string into single files?
The end result would be a directory containing the following files.
weekly.csv
any_string.csv
...
Research:
I have the following code snippet that merges all all files.
<?php
$files = glob("*.csv");
$out = fopen("merged_files.csv", "w");
foreach($files as $file){
fwrite($out, file_get_contents($file));
}
fclose($out);
echo "files merged";
?>
How can I amend my script to achieve the above?
I know I may have to use an array function, a str_
function and possibly a regular expression function.