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