I have a txt file generated by an instrument. The file needs some sanitation before the data is read into a mysql table. I have done this. But now due to some problem the data generated by the instruments contains empty lines in between and I want to remove it.
At present this is the code
$target=$_FILES["report_file"]["tmp_name"];
$file_contents = file_get_contents($target);
$file_contents = str_replace("?","",$file_contents);
$file_contents = str_replace(" ","",$file_contents);
$file_contents = str_replace("","",$file_contents);
$file_contents = str_replace("~","",$file_contents);
$file_contents = str_replace("","",$file_contents);
$file_contents = str_replace("#","",$file_contents);
$file_contents = str_replace(","," ",$file_contents);
file_put_contents($target,$file_contents);
$f = fopen($target, "r");
Can I use file_skip_empty_lines with file_put_contents? of how to put contents without empty lines?
You can trim empty lines using preg_replace
Trim whitespaces at the beginning of line
$file_contents = preg_replace("!^s+!m", "", $file_contents);
Remove empty lines
$file_contents = preg_replace("![
]+\s*[
]+!", "
", $file_contents);