Replace a whole line where a particular word is found in a text file

One approach that you can use on smaller files that can fit into your memory twice: $data = file(‘myfile’); // reads an array of lines function replace_a_line($data) { if (stristr($data, ‘certain word’)) { return “replacement line!\n”; } return $data; } $data = array_map(‘replace_a_line’, $data); file_put_contents(‘myfile’, $data); A quick note, PHP > 5.3.0 supports lambda functions … Read more