Skip to content
Advertisement

fgetcsv encoding issue (PHP)

I am being sent a csv file that is tab delimited. Here is a sample of what I see:

JavaScript

I wrote a script to open, read and loop over the values but I get weird stuff after:

JavaScript

Here is what I get when I dump the first line.

JavaScript

I tried adding:

JavaScript

And when I dump mb_detect_encoding($data[2]), I get ‘ASCII’…

Any way to fix this so I don’t have to manually update the file each time I receive it? Thanks!

Advertisement

Answer

Looks like the file is in UTF-16 (every other byte is null).

You probably need to convert the whole file with something like mb_convert_encoding($data, "UTF-8", "UTF-16");

But you can’t really use fgetcsv() in that case…

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement