php - PHP 在每一行的末尾附加一行来自另一个文本
问题描述
我有两个文本文件。第一个包含:
1,
1,
1,
1,
第二个包含:
3,4,5,
6,7,8,
0,8,9,
12,4,6,
我想得到的输出是:
1,3,4,5,
1,6,7,8,
1,0,8,9,
1,12,4,6,
基本上是用 PHP 在第一个文本文件的每一行末尾附加第二个文本文件中的一行。
$handle = fopen("data/data1.txt", "r");
//what needs to be appended
$fileContents = file_get_contents('output.txt');
$fixedFileContents = preg_replace('/.+/', '$0$handle', $fileContents);
file_put_contents($fixedFileContents, 'output.txt.txt');
代码完全错误......提前谢谢你!
解决方案
为此使用file()
$first_page_array = file("data/data1.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$second_page_array = file("provide second file path",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$final_array = [];
foreach($first_page_array as $key=>$first_page){
$final_array[] = $first_page.','.$second_page_array[$key];
}
print_r( $final_array);
file_put_contents('output.txt.txt',$final_array);
推荐阅读
- jwt - 安全方面,在本地存储中加密和存储 jwt 令牌是否足够?
- node.js - 在 Express js 中,如何正确结束抛出错误的流响应
- css - 我在设置 coreui 管理模板时遇到问题
- c++ - 将 c++ 库与 cmake 链接的问题
- amazon-web-services - 扩大规模时 Cloudwatch 不发送电子邮件通知
- visual-studio-code - 滚动鼠标滚轮时VSCode编辑器窗口间歇性跳到底部
- ffmpeg - 有没有办法用ffmpeg“裁剪”基于位图的字幕?
- go - 在包含 ANSI 转义码的字符串中的第 80 个字符后自动添加新行
- kubernetes - 如何使用 Terraform 创建一个健康的 VPC-Native GKE 集群?
- visual-studio-code - VS Code 和 Jupyter Notebook - 如何在文本编辑器中打开大输出?