Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active February 26, 2021 08:53
Show Gist options
  • Save FrancoStino/b8095160acbe81559e5a25802f74c5fa to your computer and use it in GitHub Desktop.
Save FrancoStino/b8095160acbe81559e5a25802f74c5fa to your computer and use it in GitHub Desktop.
Merge multilple file TXT into one with separator.php
<?
$anagrafiche = file('Anagrafica_Articoli.txt'); // read
$livello1 = file('Livello1.txt', FILE_IGNORE_NEW_LINES); // read
$livello2 = file('Livello2.txt', FILE_IGNORE_NEW_LINES); // read
$livello3 = file('Livello3.txt', FILE_IGNORE_NEW_LINES); // read
$livello4 = file('Livello4.txt', FILE_IGNORE_NEW_LINES); // read
$livello5 = file('Livello5.txt', FILE_IGNORE_NEW_LINES); // read
// Assuming both files have equal amount of rows.
for($x = 0; $x < count($anagrafiche); $x++) {
$anagrafiche[$x] = str_replace(array("\n", "\r"), "", $anagrafiche[$x]);
$files3[$x] = $anagrafiche[$x]."".$livello1[$x]."".$livello2[$x]."".$livello3[$x]."".$livello4[$x]."".$livello5[$x];
}
$result = implode("|\n", $files3);
// combines the array to a single string.
if(file_put_contents('prova.txt', $result)) { // puts the imploded string into file3.txt
echo "Writing to file 'prova.txt' was successfull.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment