Reputation: 97
I have a dataset which needs to be imported into an application via CSV. The problem I have is that the \r\n characters used by the program to denote a new line, aren't being exported.
MySQL recognises that they're in the database, but PHP can't seem to export them into the text file, it executes them instead. Can anyone point me in the right direction here? I'm a bit lost.
The Variable:
$elec_desc = "Value Used " . $row['item_no'] . " From " . date('d-m-Y', strtotime($readings[3])) . " to " . date('d-m-Y', strtotime($readings[1])) . '\\r\\n' . "Prev Reading " . $readings[2] . " QTY Type New Reading " . $readings[0] . " QTY Type " . '\\r\\n' . "Total Usage " . $totalused . " QTY Type " . '\\r\\n' . "Price per QTY Type \$$usagefee";
Prepared statements aren't used at this point, I was planning to modify things later once I had everything working correctly.
The export:
if ($res[$price] != 0.00){
$string_to_push = $res['inv_no'] . "," . $res['inv_date'] . "," . $sres['name'] . "," . $res[$acc] . "," . $res[$desc] . "," . $res[$gst] . "," . round((($res[$price]/11)*10),2) . "," . round(($res[$price]/11),2) . ",E";
array_push($array_of_invs, $string_to_push);
} else {
$i = 11;
}
}
}
$prevsub = "IV000000";
for($inc=0;$inc<sizeof($array_of_invs);$inc++){
$file_name = $curr_date . "_inv_export.txt";
$string = $array_of_invs[$inc];
if ($prevsub == substr($string,0,8)){
$myfile = file_put_contents($file_name, $string.PHP_EOL , FILE_APPEND | LOCK_EX);
} else {
$myfile = file_put_contents($file_name, PHP_EOL, FILE_APPEND | LOCK_EX);
$myfile = file_put_contents($file_name, ($string.PHP_EOL) , FILE_APPEND | LOCK_EX);
}
$prevsub = substr($string,0,8);
}
Upvotes: 1
Views: 1243
Reputation: 97
Seems the solution was simple enough. Escaping them once wasn't enough since they were contained within a variable, the answer was to escape them twice. Never thought to go down that road since I thought adding a third slash in would just execute them.
\\\r\\\n
^ Seemed to do the trick as they stored correctly in MySQL and exported correctly.
Upvotes: 1