0

Use code below to delete a line from a text file. If the deleted item is NOT the last element, it works fine. Deletes the line entirely.

    $panel_dir = 'host.txt';
    $panel_data = file($panel_dir);
    $panel_out = array();
    foreach($panel_data as $panel_line) {
        if(trim($panel_line) != $panel_del) {
            $panel_out[] = $panel_line;
        }
    }

    $f_panel = fopen($panel_dir, "w+") or die("Error");
    foreach($panel_out as $panel_line) {
        fwrite($f_panel, $panel_line);
    }
    fclose($f_panel);  
}

BUT if the deleted item is the LAST element, it will remove the line and then leave a blank line.

Open to better methods for deleting the entire line from text file.

Vimmy
  • 71
  • 1
  • 7
  • That's not a blank line, it's just the newline at the end of the last line, with nothing after it. – Barmar Feb 29 '20 at 19:11
  • Every line in a text file should end with a newline -- this is a requirement on Unix. – Barmar Feb 29 '20 at 19:12
  • 1
    [Didn't you kind of ask this already?](https://stackoverflow.com/q/60462265/1415724) – Funk Forty Niner Feb 29 '20 at 19:14
  • @FunkFortyNiner Entirely different issue. Using the same code for a different event. Here ayh have space ayh don't want. There ayh want a line break where ayh do want. Its been g d confusing! – Vimmy Feb 29 '20 at 19:15
  • @Barmar When ayh write the file to begin with, there is no newline after. Which is why ayh don't understand why there is this time. Is there a method for removing the last blank line? – Vimmy Feb 29 '20 at 19:26
  • ah ok, thanks @Vimmy I had to ask. – Funk Forty Niner Feb 29 '20 at 19:33

2 Answers2

1

The last line of your file doesn't end with a newline. This is bad practice on Unix, as many text-processing tools expect every line to end with newline, even the last line. But if that's what you want, you need to remove the newline at the end of the last element of $panel_out before writing to the file.

$last = count($panel_out)-1;
if ($last >= 0) {
    $panel_out[$last] = rtrim($panel_out[$last]);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Won't this then remove the last line when you remove a line higher up? When you delete a line elsewhere in the file, it does every thing right. It only effects the last line when that specific one is deleted. – Vimmy Feb 29 '20 at 19:58
  • Ayh guess ayh could add an if where it judges if this is the last element so then it runs what you wrote – Vimmy Feb 29 '20 at 19:58
  • Why would this remove any line? It's just removing the newline at the end of the last line. If you removed some other line, the last line won't have a newline, so it won't remove anything. – Barmar Mar 01 '20 at 00:34
1

Use the array_map to trim all the lines. Use the unset to delete the matching item from the array. You don't need to use the $panel_out variable. Then use the implode to turn the array into a string separated by a new line.

$panel_dir = 'host.txt';
$panel_data = array_map('trim', file($panel_dir));

foreach($panel_data as $key => $panel_line) {
    if($panel_line == $panel_del) {
        unset($panel_data[$key]);
    }
}

$f_panel = fopen($panel_dir, "w+") or die("Error");
fwrite($f_panel, implode("\n", $panel_data));
fclose($f_panel);
stillatmylinux
  • 1,399
  • 13
  • 25