【问题标题】:Php : Separate file content into different files [closed]Php:将文件内容分成不同的文件[关闭]
【发布时间】:2021-02-27 17:17:03
【问题描述】:

我有这个文件

1 + 3 = 4
0 + 0 = 0
1 + 2 = 3
1 / 1 = 1
2 * 3 = 6

我想将 (firstNum, secondNum, operationUsed(+,-,*,/), and result) 分成 4 个不同的文件

【问题讨论】:

  • 解析它然后使用file_put_contents,但是为什么呢?
  • 最简单的方法是在换行符上展开,然后循环该结果 - 并在空格上展开 - 然后将第二个 explode() 中的每个值附加到单独的文件中。

标签: php html file file-handling filehandle


【解决方案1】:

不知道你为什么会需要这个,但你去吧

<?php
// read
$array = file('original.txt');

// parse
$set = [
 'firstNum' => [],
 'secondNum' => [],
 'operationUsed' => [],
 'result' => [],
];
foreach ($array as $value) {
    $value = explode(' ', $value);
    
    $set['firstNum'][] = $value[0];
    $set['secondNum'][] = $value[1];
    $set['operationUsed'][] = $value[2];
    $set['result'][] = $value[3];
}

// write
file_put_contents('firstNum.txt', implode(PHP_EOL, $set['firstNum']));
file_put_contents('secondNum.txt', implode(PHP_EOL, $set['secondNum']));
file_put_contents('operationUsed.txt', implode(PHP_EOL, $set['operationUsed']));
file_put_contents('result.txt', implode(PHP_EOL, $set['result']));

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多