简体   繁体   中英

PHP - Shift lines in txt file to restricted amount of lines

I am writing a chat in PHP and I want to restrict the amount of lines in a chat file:

<?php
    $filename = "chat.txt";

    file_put_contents($filename, file_get_contents($filename) . $_POST["message"] . "\r\n");

    $max_lines = 3;
    $lines = 0;

    $handle = fopen($filename, "r");
    while (!feof($handle)) {
      $line = fgets($handle);
      $lines++;
    }
    fclose($handle);

    $lines_over = $lines - $max_lines;
    if ($lines_over > 0) {
        while ($lines_over > 0) {
            file_put_contents($filename, implode("\r\n", file($filename, FILE_IGNORE_NEW_LINES)));

            $lines_over--;
        }
    }
?>

, I want to delete as many lines as needed in the beginning to make the file only have 3 lines.

Which apparently doesn't work with my current code and I don't know why, please help?

I didn't fully understand the logic you are trying use. If you want to delete lines from the beginning of the file you can use this. I have added explanation in the code.

$filename = "chat.txt";
$max_lines = 3;
//Read lines into an array
$lines = file($filename);
//trim your array to max_lines
$lines = array_slice($lines, -1 * $max_lines);
//check if we have max_lines
if (count($lines) == $max_lines){
    //remove one from the beginning
    array_shift($lines);
}
//add your new line
$lines[] = trim($_POST["message"]) . "\r\n";
file_put_contents($filename, implode($lines));

References: file , array_shift and array_slice

Edit: Another method without using array_shift

$filename = "chat.txt";
$max_lines = 3;
//Read lines into an array
$lines = file($filename);
//trim your array to just one less item than max lines
$lines = array_slice($lines, -1*($max_lines-1));
//confirm newline is available in the last line
$lines[count($lines)-1] = trim($lines[count($lines)-1]) . "\r\n";
//add your new line with newline
$lines[] = trim($_POST["message"]) . "\r\n";
file_put_contents($filename, implode($lines));

Edit2: Fixed issue with empty file and file not yet created.

$filename = "chat.txt";
$max_lines = 3;
//Read lines into an array only if file_exists
//On first run this file may not be there
if(file_exists($filename)){
    $lines = file($filename);
}
//trim your array to just one less item than max lines
//This is needed only if there are lines
if (!empty($lines)){
    $lines = array_slice($lines, -1*($max_lines-1));
    //confirm newline is available in the last line
    $lines[count($lines)-1] = trim($lines[count($lines)-1]) . "\r\n";
}
//add your new line with newline
$lines[] = trim($_POST["message"]) . "\r\n";
file_put_contents($filename, implode($lines));

Note: I have not added validation for preventing browser refresh and same message getting added multiple times. This is left for you to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM