file_put_contents(
"new_file.txt",
implode(
"",
array_filter(
file("old_file.txt")
))
);
This code first reads the file to an array (file()), filters out the empty elements (array_filter) then writes them to a new file. The implode delimiter is empty, as file leaves the \n characters at the end of each line.