Reputation:
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!
Upvotes: 2
Views: 11217
Reputation: 1183
U can checkout this: https://github.com/bircher/php-merge
When working with revisions of text one sometimes faces the problem that there are several revisions based off the same original text. Rather than choosing one and discarding the other we want to merge the two revisions.
Git does that already wonderfully. In a php application we want a simple tool that does the same. There is the xdiff PECL extension which has the xdiff_string_merge3 function. But xdiff_string_merge3 does not behave the same way as git and xdiff may not be available on your system.
PhpMerge is a small library that solves this problem. There are two classes: \PhpMerge\PhpMerge and \PhpMerge\GitMerge that implement the \PhpMerge\PhpMergeInterface which has just a merge method.
PhpMerge uses SebastianBergmann\Diff\Differ to get the differences between the different versions and calculates the merged text from it. GitMerge uses GitWrapper\GitWrapper, writes the text to a temporary file and uses the command line git to merge the text.
Upvotes: 0
Reputation: 24502
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));
Upvotes: 1
Reputation: 9857
The File Append answer above is the best way. Here is a source for you to reference http://w3schools.com/php/php_file.asp
Upvotes: 0
Reputation: 2864
From your function, you can do this:
$newf3 = array();
$to = count($newf2);
for ($i=0; $i<$to; $i++) {
$newf3[] = $newf2[$i] . $newf1[$i];
}
print_r($newf3);
Upvotes: 1
Reputation: 32730
Try this : You need to give third parameter FILE_APPEND
or else it will overwrite the file.
file_put_contents('countryenglish2.txt', implode("\n", $newf2), FILE_APPEND);
ref: http://php.net/manual/en/function.file-put-contents.php
Upvotes: 1