PHP:如何在合并三个或更多文件中的每一行后将正确的输出到文件中?

我正在尝试合并三个或更多文件中的所有行。 每行必须与这些文件中的每一行合并。 每个文件可以有任意数量的行。</ p>

文件内容如下:</ p>

  file1.txt:
file1 line1 \ n file1 line2
file2.txt:
file2 line1
file2 line2
file3.txt:
file3 line1
file3 line2
</ code> </ pre>

作为PHP新手 我写了这样一段代码:</ p>

 &lt;?php 
$ file1 = file(“file1.txt”);
$ file2 = file(“file2。 txt“);
$ file3 = file(”file3.txt“);
$ file777 = fopen(”file777.txt“,”w“);
for($ f1 = 0; $ f1&lt; count($ file1); $ f1 ++)
for($ f2 = 0; $ f2&lt; count($ file2); $ f2 ++)
for($ f3 = 0; $ f3&lt; count($ file3); $ f3 ++)
fwrite($ file777,“$ file1 [$ f1] $ file2 [$ f2] $ file3 [$ f3]”);
nclclose($ file777);
?&gt;
</ code> </ pre> \ n

但是在结果文件中我得到了这样的混乱:</ p>

  file1 line1 
file2 line1
file3 line1
file1 line1
file2 line1
file3 line2file1 line1
file2 line2 file3 line1
file1 line1
file2 line2 file3 line2file1 line2 file2 line1
file3 line1
file1 line2 file2 line1
file3 line 2file1 line2 file2 line2 file3 line1
file1 line2 file2 line2 file3 line2
</ code> </ pre>

取代所需的结果:</ p>

 <  code> file1 line1 file2 line1 file3 line1 
file1 line1 file2 line1 file3 line2
file1 line1 file2 line2 file3 line1
file1 line1 file2 line2 file3 line2
file1 line2 file2 line1 file3 line1
file1 line2 file2 line1 file3 line2
file1 line2 file2 line2 file3 line1

file1 line2 file2 line2 file3 line2
</ code> </ pre>

您能否帮助我获得正确的输出?</ p>

提前致谢! </ p>
</ div>

展开原文

原文

I'm trying to merge all the lines from three or more files. Each line must be merged with every single line from these files. Each file could have any amount of lines.

Content of files is like this:

file1.txt:
    file1 line1
    file1 line2
file2.txt:
    file2 line1
    file2 line2
file3.txt:
    file3 line1
    file3 line2

As a PHP newbie I've wrote such a code:

<?php
$file1 = file("file1.txt");
$file2 = file("file2.txt");
$file3 = file("file3.txt");
$file777 = fopen("file777.txt", "w");
for($f1=0;$f1<count($file1);$f1++)
    for($f2=0;$f2<count($file2);$f2++)
        for($f3=0;$f3<count($file3);$f3++)
            fwrite($file777, "$file1[$f1] $file2[$f2] $file3[$f3]");
fclose($file777);
?>

But in the result file I'm getting a mess like this:

file1 line1
 file2 line1
 file3 line1
file1 line1
 file2 line1
 file3 line2file1 line1
 file2 line2 file3 line1
file1 line1
 file2 line2 file3 line2file1 line2 file2 line1
 file3 line1
file1 line2 file2 line1
 file3 line2file1 line2 file2 line2 file3 line1
file1 line2 file2 line2 file3 line2

Instead of a needed result like this:

file1 line1 file2 line1 file3 line1
file1 line1 file2 line1 file3 line2
file1 line1 file2 line2 file3 line1
file1 line1 file2 line2 file3 line2
file1 line2 file2 line1 file3 line1
file1 line2 file2 line1 file3 line2
file1 line2 file2 line2 file3 line1
file1 line2 file2 line2 file3 line2

Could you help me to get a proper output please?

Thanks in advance!

dongpi0658
dongpi0658 刚刚意识到文件()函数也可以使用FILE_IGNORE_NEW_LINES标志,因此输出是正确的。
大约 4 年之前 回复

2个回答



您没有删除每行末尾的换行符。 </ p>

从php.net手册中获取文件()</ p>

返回数组中的文件。 数组的每个元素对应于文件中的一行,仍然附加换行符</ strong>。 失败时,file()返回FALSE。</ p>

使用trim删除换行符。 </ p>

 &lt;?php 
$ file1 = file(“file1.txt”);
$ file2 = file(“file2.txt”);
$ file3 = file(“file3.txt”);
$ file777 = fopen(“file777.txt”,“w”);
for($ f1 = 0; $ f1&lt; count($ file1); $ f1 ++)
for($ f2 = 0; $ f2&lt; count($ file2); $ f2 ++)
for($ f3 = 0; $ f3&lt; count($ file3); $ f3 ++)
fwrite($ file777,trim($ file1 [$ f1])。“”。trim($ file2 [$ f2])。“”。trim($ file3 [$ f3])。PHP_EOL);
nclclose($ file777);
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

You're not removing the newlines at the end of each line.

From the php.net manual for file()

Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.

Use trim to remove the newlines.

<?php
$file1 = file("file1.txt");
$file2 = file("file2.txt");
$file3 = file("file3.txt");
$file777 = fopen("file777.txt", "w");
for($f1=0;$f1<count($file1);$f1++)
    for($f2=0;$f2<count($file2);$f2++)
        for($f3=0;$f3<count($file3);$f3++)
            fwrite($file777, trim($file1[$f1]) ." ". trim($file2[$f2] ) ." ".  trim($file3[$f3]) .PHP_EOL );
fclose($file777);
?>

douzhan2027
douzhan2027 刚刚意识到还有另一种方法可以解决我的问题 - 对文件()函数使用FILE_IGNORE_NEW_LINES标志,因此输出将是正确的。
大约 4 年之前 回复
duancaiyi7567
duancaiyi7567 作品完美! 非常感谢!
大约 4 年之前 回复



在调用file()时,输出包含数组每个元素中的换行符。 所以,你需要在写完之后修剪()每一行:</ p>

  fwrite($ file777,trim($ file1 [$ f1])。“”。terim($ file2 [  $ f2])。“”。($ file3 [$ f3])); 
</ code> </ pre>

最后一个没有被修剪,以保持后面的换行符。 </ p>
</ div>

展开原文

原文

Whe you call file() the output includes the newline character in each element of the array. So, you need to trim() each line after writing:

fwrite($file777, trim($file1[$f1])." ".trim($file2[$f2])." ".($file3[$f3]) );

The last one was not trimmed, to keep the newline character after it.

dopuzf0898
dopuzf0898 感谢您的帮助!
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐