簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM