If you are using * nix, you can use sed ; I find it tidier than playing with file descriptors, etc .:
exec("sed -i '/^$username,.\+\$/$username,$userpwd/g' ./users.txt 2>&1", $output, $return);
If I did not agree with GordonM and parse the file in the PHP data structure, process it and then return it:
$data = file_get_contents('./users.txt'); $users = array_map(function($line) { return explode(',', $line); }, explode("\n", $data)); foreach ( $users as $i => $user ) { if ( $user[0] == $username ) { $user[1] = $userpwd; $users[$i] = $user; } } file_put_contents('./users.txt', implode("\n", array_map(function($line) { return implode(',', $line); }, $users)));
Of course, there are many ways to do this!
source share