1
I have a txt file and need to delete the last line, but it is empty.
I tried the code below and it just erases the top line of the last one that would be blank.
Follows code:
$lines = file('file.txt');
$last = sizeof($lines) - 1 ;
unset($lines[$last]);
$fp = fopen('file.txt', 'w');
fwrite($fp, implode('', $lines));
fclose($fp);
Already tried using regular expression ?
– Mauro Alexandre
If you remove the \n of the last element would not solve?
– Eduardo Breno
I tried this expression
$string = preg_replace('/^\s*$/m', '/n', $string);
– KevinF
file_put_contents('file.txt',
 implode('', file('file.txt', FILE_SKIP_EMPTY_LINES)));
check if it solves.– Mauro Alexandre
Didn’t work out .
– KevinF
and if in place of
implode
make awhile
and treat that line ? a little more processing, but I think it solves.– Augusto
have tried
rtrim( join('', $lines ) )
?– Ricardo Moraleida
No, I’ve never tried, but it’s not really a space it’s an empty blank line.
– KevinF