【问题标题】:Replace valuie in text file with php用php替换文本文件中的值
【发布时间】:2017-08-01 09:58:32
【问题描述】:

我有一个具有以下设计的文本文件:

约翰·史密斯|1|john@smith.com|nopassword|admin
汤姆·史密斯|3|tom@smith.com|admin123|用户
....

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

汤姆·史密斯|5|tom@smith.com|admin1234|admin

我成功找到了该行并使用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]); 

【问题讨论】:

  • 你能把你现有的代码放在这里吗?
  • 您是要全部修改,还是只修改一个?
  • 另外,您是否考虑过使用数据库?这看起来更像是数据库的工作。
  • $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]); }
  • 一个数据库没关系,但这更容易使应用程序在工作中具有可移植性。

标签: php string file text replace


【解决方案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));

【讨论】:

    【解决方案2】:

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

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

    这是来自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);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 2012-08-07
      • 2015-05-13
      • 2013-09-30
      相关资源
      最近更新 更多