I've this function to remove lines from a txt file:
function replace_file($path, $string, $replace)
{
set_time_limit(0);
if (is_file($path) === true)
{
$file = fopen($path, 'r');
$temp = tempnam('./', 'tmp');
if (is_resource($file) === true)
{
while (feof($file) === false)
{
file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
}
fclose($file);
}
unlink($path);
}
return rename($temp, $path);
}
Usage: replace_file("file.txt", "line to remove", '');
It is working perfectly but it always keep an empty line at the end of the file.
Is it possible to solve that?
Any help would be appreciated.
Thanks!