代碼如下
$file_path = '123.txt';
$content = file_get_contents($file_path);
//按換行符把全部內容分隔成數組
$con_array = explode("n", $content);
//替換掉指定行
$con_array[12]="123";
//組合回字符串
$con = implode("n", $con_array);
//寫回文檔
file_put_contents($file_path, $con);