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

4

8

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:

//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    
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

//Users

//Something Else

//Something Else 2

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

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

Parrie answered 16/5, 2013 at 21:30 Comment(2)
I think when your file manipulation starts to be a little bit complex, you should use a database. Try SQLite, should suits you perfectly...Handling
This was just a example, I'm going to use it for something else. Thanks for info tho.Parrie
J
7

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);
Jun answered 16/5, 2013 at 22:37 Comment(0)
G
0

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.

Glochidium answered 16/5, 2013 at 21:46 Comment(0)
K
0

Try 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

Kaylil answered 16/5, 2013 at 21:46 Comment(0)
D
0

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

Digged answered 16/5, 2013 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.