S
S
Sergey Kiryanov2019-10-17 15:33:28
PHP
Sergey Kiryanov, 2019-10-17 15:33:28

How to fix encoding in CSV?

I write to a file and upload CSV in this way:

$data = [
  'Товар 1;some;3',
  'Товар 2;anather;6',
  'Товар 3;test;9'
];

header('Content-Type: text/csv');
header('Content-disposition: attachment;filename=data.csv');
foreach ($data as $row) {
  echo $row, PHP_EOL;
}

At the output, I get krakozyabry in Russian characters . I
tried it with the iconv("utf-8", "windows-1251", $string) function;
Displays Russian characters, but the first character is lost or distorted. "Set" instead of "Set"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
irishmann, 2019-10-17
@Dizzy221

<?php
    $data = [
      'Товар 1;some;3',
      'Товар 2;anather;6',
      'Товар 3;test;9'
    ];

    header('Content-Type: text/csv');
    header('Content-disposition: attachment;filename=data.csv');
    foreach ($data as $row) {
      echo mb_convert_encoding($row, 'CP1251', mb_detect_encoding($row)), PHP_EOL;
    }

A
Araik, 2019-10-17
@NinjaNickName

Try saving the file itself in UTF-8 encoding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question