Here is some detail:
1. Create the text file.
2. Add unique identifier to that file (#end)
3. Use filesystem fuctions to open the file.
4. Use ereg_replace function to find and replace your identifier.
5. Save the file.
<?php
$myTextfile = "/path/to/textfile";
$myTextfilesize = filesize($myTextfile);
$myIndex = fopen($myTextfile,"r+") or die ("file $myTextfile not available");
$textfileContent = fread($myIndex,$myTextfilesize) or die ("cannot read $myTextfile");
$newContent = ereg_replace("#end","$newData #end",$textfileContent);
fput($myIndex,$newContent);
fclose($myIndex);
?>
If any mistakes, check the syntax.