PHP - Export query to CSV wrong character on date field -
i'm trying create csv file mysql query, have problem dates. need force enclose each fields double quotes, , i'm using function:
$fp = fopen($filename, 'w'); $result = mysqli_query($db_conn, $query) or die( mysqli_error( $db_conn ) ); while($row = mysqli_fetch_assoc($result)) { //fputcsv($fp, $row); fputcsv($fp, array_map(function($value) { if ($value == null || $value == "" || !isset($value) || $value === "0000-00-00 00:00:00") { return '""'; } ///remove escaped double quotes within string. $value = str_replace('\\"','"',$value); //then force escape these same double quotes , unescaped ones. $value = str_replace('"','\"',$value); // remove \n $value = str_replace("\n", '', $value); $value = str_replace("\r", '', $value); $value = str_replace("\r\n", '', $value); //force wrap value in quotes , return return "\"$value\""; }, $row), ',', chr(0)); } fclose($fp);
the export works (i think...) fields except date field. whe try read file in way:
while (($data = fgetcsv($handle, null, ",", '"', '"')) !== false) { // someting }
all fields double quoted but, dates field have content:
"\x00"2008-12-31 10:08:43"\x00"
i tried many ways solve but.. without result..
i had similar problem , figured out single space character can crash csv. go through this link maybe figure out problem.
Comments
Post a Comment