【发布时间】:2016-05-30 10:12:17
【问题描述】:
Ongoing Series
Series 2
Series 1
我想在第一行和第二行之间插入系列 3,并使用 file_get_contents file_put_contents 将系列 2 替换为系列 3 \n 系列 2,但没有成功
【问题讨论】:
Ongoing Series
Series 2
Series 1
我想在第一行和第二行之间插入系列 3,并使用 file_get_contents file_put_contents 将系列 2 替换为系列 3 \n 系列 2,但没有成功
【问题讨论】:
$flename = "filename";
$newString = "series 3";
// Read file to array
$lines = file($filename);
// Add line after the fist one
array_splice($lines, 1, 0, $newString . PHP_EOL);
// Write back
file_put_contents($filename, implode('', $lines));
【讨论】:
$text = file_get_contents("myfile.txt")
$text = preg_replace('/Series 2\s+/si', 'Series 2\nSeries 3\n', $text);
file_put_contents("newfile.txt", $text);
【讨论】: