简体   繁体   English

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

[英]replace string with string+new line in file

Ongoing Series
Series 2
Series 1

I want to insert series 3 between first and second line with replacing series 2 with series 3 \\n series 2 using file_get_contents file_put_contents with out success 我想在第一和第二行之间插入系列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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM