Jump to content

Problem with Hard Returns in CSV Export


ShorStew

Recommended Posts

The code below has been working on my site for almost three years, until recently. Now the resulting CSV has unwanted rows where there is a hard return in one of fields. I am not sure if this is due to a change in settings (I'm on a shared server) or a new version of PHP (currently 5.1.6).

 

Any thoughts? Thanks in advance.

 

<?php

require ('./includes/mysql_connect.php');


$select = stripslashes($_GET['selection']);
$export = mysql_query($select);
$fields = mysql_num_fields($export);
$header = '';
$data = '';

for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "\t";
}


while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";
}

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

mysql_free_result($export);

mysql_close();

?>

Link to comment
https://forums.phpfreaks.com/topic/96980-problem-with-hard-returns-in-csv-export/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.