Reputation: 1
I've been trying to export MySQL fields to CSV and can get all them exporting correctly except the filed which contains HTML.
My MySQL table is made up as follows:
Name: Address: Profile:
Name is simply text as with address, but profile is HTML
My array is:
$array = array ( array ( "Name" => "$name", "Address" => "$address", "Profile" => $profile )
);
Made using:
$name = $info['name'];
$address = $info['address'];
$profile = $info['profile'];
I have a "Tab" \t delimiter and every exports into the CSV correctly bar the HTML which I would like to keep as pure HTML but in one Excel field, at the moment is splits across multi rows and fields.
CSV code is as follows:
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
$titleArray = array_keys($array[0]);
$delimiter = "\t";
$filename="profiles.xls";
foreach ($array as $subArrayKey => $subArray) {
$dataRowString = implode($delimiter, $subArray);
print $dataRowString . "\r\n";
}
Any help or advise would be highly welcomed.
Upvotes: 0
Views: 80
Reputation: 57719
You need to escape the field values. You can use fputcsv
:
$stdout = fopen('php://stdout', 'w');
fputcsv($stdout, $subArray, "\t");
$filename="profiles.xls";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
$titleArray = array_keys($array[0]);
$delimiter = "\t";
$stdout = fopen('php://stdout', 'w');
foreach ($array as $subArrayKey => $subArray) {
fputcsv($stdout, $subArray, $delimiter);
}
Upvotes: 1