【发布时间】:2013-05-11 23:02:51
【问题描述】:
我是新来的。
无论如何,我对 fwrite() 进行了研究,但找不到解决方案,所以我正在寻求帮助。
我想要的是 f.e.在其他特定行之后添加新的文本行。
F.e.我有一个 .txt 文件,其中有:
//Users
//Other stuff
//Other stuff2
现在我想做的是能够在 //Users 下方添加一个新用户,而无需触摸“Other Stuff”和“Other Stuff 2”。所以它应该看起来像这样:
//Users
Aneszej
Test321
Test123
//Other stuff
//Other stuff2
到目前为止我所拥有的:
$config = 'test.txt';
$file=fopen($config,"r+") or exit("Unable to open file!");
$date = date("F j, Y");
$time = date("H:i:s");
$username = "user";
$password = "pass";
$email = "email";
$newuser = $username . " " . $password . " " . $email . " " . $date . " " . $time;
while (!feof($file)) {
$line=fgets($file);
if (strpos($line, '//Users')!==false) {
$newline = PHP_EOL . $newuser;
}
}
fwrite($file, $newline);
fclose($file);
test.txt 文件
//Users
//Something Else
//Something Else 2
但这只会将用户写入 .txt 文件的末尾。
非常感谢大家的帮助!解决了。
【问题讨论】:
-
我认为当您的文件操作开始有点复杂时,您应该使用数据库。试试 SQLite,应该很适合你...
-
这只是一个例子,我打算用它来做其他事情。感谢您提供信息。