title: PHP Modify a single line in a text file
body: <p>I tried and looked for a solution, but cannot find any definitive.</p>
  
  <p>Basically, I have a txt file that lists usernames and passwords. I want to be able to change the password of a certain user.</p>
  
  <p>Contents of users.txt file:</p>
  
  <pre><code>user1,pass1
  user2,pass2
  user3,pass3
  </code></pre>
  
  <p>I've tried the following php code:</p>
  
  <pre><code>            // $username = look for this user (no help required)
              // $userpwd  = new password to be set 
  
      $myFile = "./users.txt";
      $fh = fopen($myFile,'r+');
  
      while(!feof($fh)) {
          $users = explode(',',fgets($fh));
          if ($users[0] == $username) {
              $users[1]=$userpwd;
              fwrite($fh,"$users[0],$users[1]");
          }
      }       
  
      fclose($fh);    
  </code></pre>