Reputation: 367
Need to convert array to display in csv file
Here is sample code
$output=array(
array('firstname' => 'latviešu', 'lastname' => 'Johnson', 'age' => 25),
array('firstname' => 'русский', 'lastname' => 'latviešu', 'age' => 25),
array('firstname' => 'šAmanda', 'lastname' => 'Miller', 'age' => 18),
array('firstname' => 'english', 'lastname' => 'русский', 'age' => 25.04),
);
header('Pragma: public');
header('Content-type: application/x-msexcel');
header('Content-Disposition: attachment; filename="utf8_bom.xls"');
foreach($output as $row) {
$implode = (implode("\t", $row)). "\r\n" ;
$implode = mb_convert_encoding($implode, 'UTF-16LE', 'UTF-8');
echo $implode = "\xFF\xFE" . $implode;
}
csv output is like this
As you see starting from the second row there is character before each word. When I copied the character in html get 
.
If remove \xFF\xFE
, no such character, but the text is not normally readable. Tried to trim
, but also not normally readable.
How to remove that character?
Solution
Here is solution finally
foreach($output as $row) {
if(!$flag) {
$implode = (implode("\t", array_keys($row))). "\r\n" ;
$implode = mb_convert_encoding($implode, 'UTF-16LE', 'UTF-8');
echo $implode = "\xFF\xFE" . $implode;
$flag = true;
}
$implode = (implode("\t", array_values($row))). "\r\n" ;
$implode = mb_convert_encoding($implode, 'UTF-16LE', 'UTF-8');
echo $implode;
}
But next question related with the code...
At the end of the array array('firstname' => 'english', 'lastname' => 'русский', 'age' => 25.04),
is 25.04
; however in csv get 25.Apr
how to fix this problem?
Upvotes: 1
Views: 1757
Reputation: 2729
I think you need to add the BOM just at the start of the file, but mb_convert_encoding
adds it at each line. If possible, try this:
$utf = '';
foreach($output as $row) {
$utf .= (implode("\t", $row)). "\r\n" ;
}
$utf = mb_convert_encoding($implode, 'UTF-16LE', 'UTF-8');
echo $utf;
Upvotes: 1
Reputation: 522195
A block of UTF-16 encoded text needs to start with a BOM. The BOM must not be repeated in the text itself. You're currently outputting a BOM for every single line. Don't do that, just output it once at the beginning of the file.
Upvotes: 3