1

I am trying to load a bunch of csv files into a database. Parsing the files and entering the data is all working great.....except some of the files encoding is giving me trouble.

UTF-8 files work fine and most files are this format. However some of the files are in ASCII and these files do not load properly.

Is there a way to check the file itself for its encoding, and convert the encoding before using the data? I have been playing with mb_convert_encoding and iconv but these all take a string as input. would rather check the file and convert right off the bat instead of checking it line by line as I take in the data.

Thank you.

eignhpants
  • 1,611
  • 4
  • 26
  • 49
  • `mb_detect_encoding()`, but it's not 100% reliable. getting the wrong source encoding and then doing the conversion will likely just corrupt the file. – Marc B Sep 09 '14 at 22:01
  • I tried this and it didn't work. In fact nothing I have tried has worked, even the string conversions. – eignhpants Sep 09 '14 at 22:08
  • Could you give an example of an 'ASCII' file that is not loading correctly? It's almost impossible for ASCII not to load properly, especially if UTF-8 is loading properly. It might be a CP-1252 file, [which I wrote a long answer on here](http://stackoverflow.com/questions/22683132/php-cp1252-windows-1252-conversion-to-utf-8/23250083#23250083). – NobleUplift Nov 13 '15 at 17:21

1 Answers1

0

Try below code:

<?php
$handle = fopen ("temp.csv","r");
while ($data = fgetcsv ($handle, 1000, ";")) {
        $data = array_map("utf8_encode", $data);
}
?>
Jayni
  • 19
  • 3