Add a new line of text after finding a particular string of text in a file using php

Hello

how to add a new line of text after finding a particular string of text in a file, using php ?
Suppose I have the file
data.txt
and I want add test after the string @add@ , how to do that ?

Thank you
Cris

Are you reading in the entire text file?

There are many ways of doing this, some better suited that others, here’s one.


<?php
$lines = array();
foreach(file('source.txt') as $line)
{
	if('@add@' === $line)
	{
		array_push($lines, 'test');
	}
	array_push($lines, $line);
}
file_put_contents('source.txt', $lines);

Fantastic , thank you , really of help