PHP修改文本文件中的一行
我试着寻找一个解决方案,但找不到任何明确的答案 基本上,我有一个txt文件,其中列出了用户名和密码。我希望能够更改某个用户的密码 users.txt文件的内容:PHP修改文本文件中的一行,php,string,file,text,Php,String,File,Text,我试着寻找一个解决方案,但找不到任何明确的答案 基本上,我有一个txt文件,其中列出了用户名和密码。我希望能够更改某个用户的密码 users.txt文件的内容: user1,pass1 user2,pass2 user3,pass3 我尝试了以下php代码: // $username = look for this user (no help required) // $userpwd = new password to be set
user1,pass1
user2,pass2
user3,pass3
我尝试了以下php代码:
// $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);
我认为当你得到那个文件时,使用file\u get\u内容,然后使用preg\u替换特定的用户名 我以前也做过类似的事情
$str = "";
$reorder_file = FILE_PATH;
$filecheck = isFileExists($reorder_file);
if($filecheck != "")
{
$reorder_file = $filecheck;
}
else
{
errorLog("$reorder_file :".FILE_NOT_FOUND);
$error = true;
$reorder_file = "";
}
if($reorder_file!= "")
{
$wishlistbuttonhtml="YOUR PASSWORD WHICH YOU WANT TO REPLACE"
$somecontent = $wishlistbuttonhtml;
$Handle = fopen($reorder_file, 'c+');
$bodytag = file_get_contents($reorder_file);
$str=$bodytag;
$pattern = '/(YOUR_REGEX_WILL_GO_HERE_FOR_REPLACING_PWD)/i';
$replacement = $somecontent;
$content = preg_replace($pattern, $replacement, $str,-1, $count);
fwrite($Handle, $content);
fclose($Handle);
}
希望这有帮助……正确的方法是使用数据库。数据库可以很容易地进行随机访问,而文本文件的访问就更少了 如果您由于任何原因无法切换到数据库,并且您的系统也不希望有超过1000个用户,那么只需读入整个文件,将其转换为PHP数据结构,进行所需的更改,将其转换回文本并覆盖原始文件,就会简单得多 在这种情况下,这意味着file()将文本文件加载到一个数组中,每个元素都是用户名和密码作为字符串,在逗号处分解数组中的所有元素以分别获取用户名和密码,进行所需的更改,然后将修改后的数据写入光盘 您可能还发现fgetcsv()对于读取数据很有用。如果您使用SplFileObject并拥有最新版本的PHP,那么也可以使用fputcsv()将数据写回
然而,仅仅使用数据库是一个更好的解决方案。适合这项工作的工具 如果您使用*nix系统,您可以使用
sed
;我发现它比玩文件句柄等更整洁:
exec("sed -i '/^$username,.\+\$/$username,$userpwd/g' ./users.txt 2>&1", $output, $return);
如果不是,我同意GordonM,将文件解析为PHP数据结构,对其进行操作,然后将其放回:
$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)));
当然,有无数种方法可以做到这一点 这应该有效!:)
您的解决方案的问题是,您将文本(顺序)文件作为二进制文件处理。您正在使用的fwrite在这里不起作用。您的问题就是为什么不应该使用平面文本文件,而应该使用数据库。此外,还存在同步问题。所以,你应该尝试使用。或者,它很容易让一些用户发疯。
$file = "./users.txt";
$fh = fopen($file,'r+');
// string to put username and passwords
$users = '';
while(!feof($fh)) {
$user = explode(',',fgets($fh));
// take-off old "\r\n"
$username = trim($user[0]);
$password = trim($user[1]);
// check for empty indexes
if (!empty($username) AND !empty($password)) {
if ($username == 'mahdi') {
$password = 'okay';
}
$users .= $username . ',' . $password;
$users .= "\r\n";
}
}
// using file_put_contents() instead of fwrite()
file_put_contents('./users.txt', $users);
fclose($fh);
$fn = fopen("test.txt","r") or die("fail to open file");
$fp = fopen('output.txt', 'w') or die('fail to open output file');
while($row = fgets($fn))
{
$num = explode("++", $row);
$name = $num[1];
$sex = $num[2];
$blood = $num[3];
$city = $num[4];
fwrite($fp, "Name: $name\n");
fwrite($fp, "Sex: $sex\n");
fwrite($fp, "Blood: $blood\n");
fwrite($fp, "City: $city\n");
}
fclose($fn);
fclose($fp);