如何在单独的行上从一个文本文件写入另一个文件


How to write from one text file to another file on separate lines

源文件的电话号码如下:

8076130581  8070200776  8071768383  8075591843

我希望目标文件有这样的数字:

08076130581
08070200776
08071768383
08075591843

我的问题是输出到目标文件的。因此:08076130581 8070200776 8071768383 807559184308070200776 8071768383 8075591843

我代码:

$source = 'Path/To/Source.txt';
$target = 'Path/To/Target.txt';
$lines = file($source);
foreach($lines as $line)
{
  /* Split each line into an array */
  $splitting = explode(' ', $line);
  foreach($splitting as $phone_number)
  {
    /* Store each array value with a leading zero into a temporary $variable */
    $each_phone_number[] = '0' . trim($phone_number) . PHP_EOL;
  }
}
/*Write the content back to another file*/
file_put_contents($target, $each_phone_number);

我已经根据源示例测试了您的脚本。

这行好像少了一个空格 $splitting = explode(' ', $line); 你应该这样写: $splitting = explode(' ', $line); 如果在source.txt中手机之间的空间是一致的,它应该解决问题并创建预期的目标文件