N
N
Nikita Souvel2016-03-31 18:10:12
PHP
Nikita Souvel, 2016-03-31 18:10:12

How to open correctly a file saved in cp1251 on a site where all the encoding is utf8?

In general, the essence is that the file is submitted and processed, it is a CSV file, and since it is saved in Excel, it is encoded in cp1251 at the output, and the entire site is encoded in utf8, how can this problem be correctly solved?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Grigory Esin, 2016-03-31
@Souvel1

For example, you can do this:

$fh = fopen("file.csv", "r");
while (false !== $row = fgetcsv($fh, ...)) {
    foreach ($row as &$rowItem) {
        $rowItem = mb_convert_encoding($rowItem, "utf8", "cp1251");
    }
    unset($rowItem);
    ...
}
fclose($fh);

PS
Toster Driven Development

I
IceJOKER, 2016-03-31
@IceJOKER

fi2.php.net/manual/en/function.mb-convert-encoding.php

A
Arman, 2016-03-31
@Arik

probably only change on the fly
php.net/manual/ru/function.iconv.php
php.net/manual/ru/function.mb-convert-encoding.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question