PHP fwrite() 如何在特定行后插入新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16597836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:25:15  来源:igfitidea点击:

PHP fwrite() how to insert a new line after some specific line

phpnewlinefwritestrpos

提问by Aneszej

I'm new here.
Anyway, I did my research on fwrite(), but I couldn't find solution, so i'm asking for help. What I want is f.e. to add a new line of text after some other specific line. F.e. I have a .txt file in which there is:

我是新来的。
无论如何,我对 fwrite() 进行了研究,但找不到解决方案,所以我寻求帮助。我想要的是 fe 在其他特定行之后添加一行新文本。Fe 我有一个 .txt 文件,其中有:

//Users

//Other stuff

//Other stuff2  

Now what I'd like to do is be able to add a new user below //Users without touching "Other Stuff" and "Other Stuff 2". So it should look something like this:

现在我想做的是能够在 //Users 下面添加一个新用户,而无需触摸“Other Stuff”和“Other Stuff 2”。所以它应该看起来像这样:

//Users    
Aneszej  
Test321  
Test123

//Other stuff

//Other stuff2  

What I have so far:

到目前为止我所拥有的:

$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 file

测试.txt文件

//Users

//Something Else

//Something Else 2

But this only writes users to the end of the .txt file.

但这只会将用户写入 .txt 文件的末尾。

Thanks a lot everyone for your help! It's solved.

非常感谢大家的帮助!解决了。

采纳答案by Ming M Zheng

I modified your code, I think follows is what you need, and I also put comment, function below will keep adding new user, you can add condition that to check user exist.

我修改了您的代码,我认为以下是您需要的,并且我还添加了注释,下面的功能将不断添加新用户,您可以添加检查用户存在的条件。

$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."\r\n";   // I added new line after new user
$insertPos=0;  // variable for saving //Users position
while (!feof($file)) {
    $line=fgets($file);
    if (strpos($line, '//Users')!==false) { 
        $insertPos=ftell($file);    // ftell will tell the position where the pointer moved, here is the new line after //Users.
        $newline =  $newuser;
    } else {
        $newline.=$line;   // append existing data with new data of user
    }
}

fseek($file,$insertPos);   // move pointer to the file position where we saved above 
fwrite($file, $newline);

fclose($file);

回答by flaschenpost

You write the new content at the end of the reading, so it has to write at the end of file - the cursor is there after reading all the lines.

您在阅读结束时写入新内容,因此必须在文件末尾写入 - 读取所有行后,光标就在那里。

Either you store all the content in php-variable and overwrite the file in the end, or you rewind the curser with fseek as mentioned by Robert Rozas comment. That should be done as soon as you read the "Something else" - line.

要么将所有内容存储在 php-variable 中并最终覆盖该文件,要么如 Robert Rozas 评论所述,使用 fseek 倒带光标。一旦您阅读“其他内容” - 行,就应该这样做。

回答by Hackerman

Try fseek:

试试 fseek:

<?php
 $file = fopen($filename, "c");
 fseek($file, -3, SEEK_END);
 fwrite($file, "whatever you want to write");
 fclose($file);
?>

PHP Doc: http://php.net/manual/en/function.fseek.php

PHP 文档:http: //php.net/manual/en/function.fseek.php

回答by Markku K.

You need to breakafter you find '//Users'. You keep reading to the end of the file.

break在找到“//Users”之后,您需要这样做。你一直读到文件的末尾。