用php替换文本文件中的valuie

时间:2017-03-10 22:33:25

标签: php string file text replace

我有一个文本文件,其设计如下:

  

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

等等。 每个字段由" |"分隔。 我需要做的是在此文本文件中替换值,例如用户的密码或帐户类型。

Tom smith | 5 | tom@smith.com | admin1234 | 管理员

我成功找到了该行并使用explode()进行吐出但是如何修改该值并将其写回文本文件?

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

2 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点。这是一种方式。我评论了代码来解释它是如何工作的。

$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("\n", $mydb));

答案 1 :(得分:0)

PHP提供了读取,写入和附加到文件的选项。

您需要打开文件,读取所有内容,然后覆盖该文件。您将使用append获得重复的条目。

以下是https://www.w3schools.com/php/php_file_create.asp的示例代码:

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

代码:

<?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("\r\n","\r"),"",$pieces[1]); 
      $email = str_replace(array("\r\n","\r"),"",$pieces[2]); 
      $password = str_replace(array("\r\n","\r"),"",$pieces[3]); 
      $atype = str_replace(array("\r\n","\r"),"",$pieces[4]); 

      // Glue the variables back together and overwrite $line so that it gets up in the array
      $line = $name & "|" $position & "|" $email & "|" $password & "|" $atype & "\r";
    }
    // 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);
?>