In text file "foo.txt" I have these:
aaa|bbb|ccc|row0
ddd|eee|fff|row1
ggg|hhh|iii|row2

I want to edit row1... say, I wanna change it to zzz|eee|xxx|row1, so the final output is:
aaa|bbb|ccc|row0
zzz|eee|xxx|row1
ggg|hhh|iii|row2

How should I accomplish this?

Thank you.

    $myfile = file('database.txt');

    foreach ($myfile as $linenum=>$lines) {

    if (stristr($line,'line2')) { $linetoedit = $lines; $linenumtoedit=$linenum} // additional processing here
    }

    $linetoedit = ....;

    $myfile[$linenumtoedit] = $linetoedit;

    $fp=fopen('database.txt','w+');
    fwrite($fp, implode("\n", $myfile);
    fclose($fp);

      For some reason, it returns a new line before the row that I want to edit...

      This is what I have originally:
      aaa|bbb|ccc|row0
      ddd|eee|fff|row1
      ggg|hhh|iii|row2

      This is how it's supposed to be after editing:
      aaa|bbb|ccc|row0
      zzz|eee|xxx|row1
      ggg|hhh|iii|row2

      But the result looks like this now for some reason:
      aaa|bbb|ccc|row0

      zzz|eee|xxx|row1
      ggg|hhh|iii|row2

      Any ideas? I can't figure out what's causing the \n
      Thanks.

        you are probably sticking one on the line you are editing.

        remember, you are imploding with the character \n as the separater... so if you are adding one in the line you are editing, then you've got two \n's..

          I'm sorry, what do you mean by "sticking one/adding one"?
          If you mean I added an additional "\n", no... I didn't. And besides, if I did, the new line will appear at the end of the row, not in front of it...

            I'm not sure why you don't just use a database...
            it would be much easier...

            
            $file = file('DB.txt');
            foreach ($file as $line=>$row) {
            $file[$line] = $row;
            $file[$id] = "$first_name|$last_name|$address|$city|$state|$zip|$home_phone|$work_phone|$wireless_phone|$email|$dob|$notes|";
            }
            $fp = fopen('DB.txt','w');
            fwrite($fp,implode("\n",$file));
            fclose($fp);
            
            
            

            I'm not sure why that is being done.
            I believe you can take out the foreach...

            
            $file = file('DB.txt');
            
            $file[$id] = "$first_name|$last_name|$address|$city|$state|$zip|$home_phone|$work_phone|$wireless_phone|$email|$dob|$notes|";
            
            $fp = fopen('DB.txt','w');
            fwrite($fp,implode("\n",$file));
            fclose($fp);
            
            
            

            also I'm not sure what's up with imploding using \n yet earlier on replacing all the \n with <br>

              This app was originally built with php & mysql, I just want to see if it's possible to convert it to a flatfile...
              I tried both: with foreach & without foreach, both produced same result... I also tried taking out the str_replace("\n","<br>", it still produced the same result (new line break before the row I want to edit)

              I've already tried everything I can think of, still have no clue why it does a \n before the row...
              (I had to replace the \n with <br> in the textarea part, else it won't show as a new line in HTML)

              All I want it to do is do the samething as sql does (update $table set first_name = '$first_name' where row = '$line')

                🙂 Just found what's adding the extra \n, it's the implode... now I gotta find another way to join the data in the array, any suggestions?

                Thanks

                  Nevermind... I fixed the problem simply by using implode("",$file); instead of using implode("\n",$file)

                  Thanks for all of your help though 🙂

                    Now I can't edit the first row $file[0]... all other rows can be edited. Any suggestions are appreciated, thanks.

                      Write a Reply...