用文件中的字符串+新行替换字符串

replace string with string+new line in file

Ongoing Series
Series 2
Series 1

我想在第一行和第二行之间插入系列 3,用系列 3 替换系列 2\n 使用 file_get_contents file_put_contents 系列 2 但没有成功

$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);
$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));