A
A
andrey_levushkin2020-09-11 13:32:03
PHP
andrey_levushkin, 2020-09-11 13:32:03

What encoding should a CSV file have for correct reading through php?

After uploading the CSV file to the server and trying to display its contents, I experience problems with the encoding and see "cracks".
Further work with these data is required.
What encoding should be specified for the csv file before uploading to the server, so that php correctly displays its contents?
Maybe you can just specify in the code what encoding is used?

If I just needed to display the content, I would write: for the appropriate encoding. But in the future I work with the contents of the file (its line-by-line moving to the data array). Which complicate the task.
header('Content-Type: text/html; charset=UTF-8');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2020-09-11
@andrey_levushkin

Now it is customary to write php code in utf-8 and give site pages in utf-8.
If the csv file is in a different encoding, recode it using iconv or similar.
$str = iconv( "Windows-1252", "UTF-8", $str );

A
Adamos, 2020-09-11
@Adamos

But in the future I work with the contents of the file (its line-by-line moving to the data array). Which complicate the task.

In fact, on the contrary, it simplifies. It is enough to look at the documentation for the fgetcsv parsing CSV function and see that the encoding for it is determined by the server locale settings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question