[英]replace string with string+new line in file
Ongoing Series
Series 2
Series 1
我想在第一和第二行之间插入系列3,并使用file_get_contents file_put_contents将系列2替换为系列3 \\ n系列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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.