用php替换文本文件中的值

i have a text file with the following design:

john smith|1|john@smith.com|nopassword|admin
Tom smith|3|tom@smith.com|admin123|user
....

And so on. Every field is delimited by "|". What i need to do is replace values for example the password or the account type of a user in this text file.

Tom smith|5|tom@smith.com|admin1234|admin

I succeeded in finding the line and spiting it with explode() but how do i modify the value and write it back on the text file ?

$name = $_POST['name'];
$mydb = file('./DB/users.txt');
foreach($mydb as $line) {
    if(stristr($line,$name)) $pieces = explode("|", $line);
    $position = str_replace(array("
",""),"",$pieces[1]);
    $email = str_replace(array("
",""),"",$pieces[2]);
    $password = str_replace(array("
",""),"",$pieces[3]);
    $atype = str_replace(array("
",""),"",$pieces[4]); 

PHP gives the options of reading, writing, and appending to files.

You need to open the file, read all content in, and then overwrite the file. You will get duplicate entries using append.

Here is an example code from https://www.w3schools.com/php/php_file_create.asp:

<?php
  $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  $txt = "John Doe
";
  fwrite($myfile, $txt);
  $txt = "Jane Doe
";
  fwrite($myfile, $txt);
  fclose($myfile);
?>

For your code:

<?php
  $name = $_POST['name'];
  $mydb = file('./DB/users.txt'); 

  $output = array();
  foreach($mydb as $line) { 
    if(stristr($line,$name)) {
      $pieces = explode("|", $line); 
      $position = str_replace(array("
",""),"",$pieces[1]); 
      $email = str_replace(array("
",""),"",$pieces[2]); 
      $password = str_replace(array("
",""),"",$pieces[3]); 
      $atype = str_replace(array("
",""),"",$pieces[4]); 

      // Glue the variables back together and overwrite $line so that it gets up in the array
      $line = $name & "|" $position & "|" $email & "|" $password & "|" $atype & "";
    }
    // put everything in the $output array so that it can be writen to file after this loop
    array_push($output, $line);
  }
  fclose($mydb);

  // Write your output to file
  $mydb = fopen('./DB/users.txt', "w") or die("Unable to open file!");
  foreach($output as $line) { 
    fwrite($mydb, $line);
  }
  fclose($mydb);
?>

There are various ways to do this. Here's one way. I commented the code to explain how it works.

$name = $_POST['name'];

// use FILE_IGNORE_NEW_LINES so you won't have to deal with the line breaks
$mydb = file('./DB/users.txt', FILE_IGNORE_NEW_LINES);

// use a reference (&$line) so the code in your foreach loop modifies the $mydb array
foreach ($mydb as &$line) {
    if (stristr($line,$name)) {

        // assign the elements of the row to variables
        list($name, $number, $email, $password, $type) = explode('|', $line);

        // change whatever you need to change
        $password = 'new password';
        $type = 'new type';

        // overwrite the line with the modified values
        $line = implode('|', [$name, $number, $email, $password, $type]);

        // optional: break out of the loop if you only want to do the replacement
        // for the first item found that matches $_POST['name']
        break;
    }
};

// overwrite the file after the loop
file_put_contents('./DB/users.txt',  implode("
", $mydb));