How to change the contents of the text file using php?


i want to store users comments of my site, in a txt file. so.. i want to know how to edit txt file content using php.

my txt file contents goes like this...

comment="Hello world"

comment="Test comment"

so..if i want to edit uid=5's comment, how can i do it with php. or tell me a better method, content should go in text file to make this task eazy.

i dont like to use databse to store my comments. pls, someone help me in this matter. Thansk

$txt_file = file_get_contents('path/to/file');
$rows = explode("\n", $txt_file); //you get all rows here
foreach ($rows as $row => &$data) {
    if (strstr($data, 'uid=5') !== FALSE) {
        //it means the following line contains your comment,
        //work with it as string
        $rows[$row + 1] = "comment=" . $newComment;
    $data = $data . "\n";
file_put_contents('path/to/file', $rows);