Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are two text files.

The first contains:
11111111
22222222
33333333
44444444

The second contains:
AAAAAAAA
BBBBBBBB
CCCCCCCC
DDDDDDDD

What I want:
11111111AAAAAAAA
22222222BBBBBBBB
33333333CCCCCCCC
44444444DDDDDDDD

But I get:
11111111
AAAAAAAA
22222222
BBBBBBBB
33333333
CCCCCCCC
44444444
DDDDDDDD


What I have tried:

The code is the following:
PHP
<?php
$lines = file('IVS_headings.txt');
$lines2 = file('IVS_links.txt');

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

file_put_contents('to_write_file.txt', $lines);
?>
Posted
Updated 26-Feb-22 3:41am
v2
Comments
Richard MacCutchan 22-Jul-21 5:50am    
Because each line that you read is terminated by a newline character. So you need to strip that final character from each string in $lines, before you add the second string to it.

1 solution

Use PHP: rtrim - Manual[^] :
PHP
foreach ($lines as $key => $val) {
  $temp = rtrim($val);
  $lines[$key] = $temp.$lines2[$key];
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900