Arnaud Ad Arnaud Ad - 1 month ago 4
PHP Question

merge two file text with php

I have two text files I have created this way:

<?php
$lines = file('Country.txt');
$newf = array();
foreach ($lines as $line)
$newf[] = substr($line, 2);
file_put_contents('country2.txt', implode("\n", $newf));


$lines2 = file('countryenglish.txt');
$newf2 = array();
foreach ($lines2 as $line)
$newf2[] = substr($line, 3);
file_put_contents('countryenglish2.txt', implode("\n", $newf2));

?>


Both display data like this:

Country2.txt:
Text1Data1
text1Data2
text1Data3
>
countryenglish2.txt
Text2Data1
text2Data2
text2Data3


I would like to display:

Text2Data1Text1Data1
text2Data2text1Data2
text2Data3text1Data3


Thanks in advance for your help!

Answer

You could try something like this:

$lines = file('Country.txt');
$lines2 = file('countryenglish.txt');

foreach ($lines as $key => $val) {
    $lines[$key] = $val.$lines2[$key];
}

file_put_contents('countryenglish2.txt', implode("\n", $lines));