Home » Php » php – upload and convert csv UTF16 to utf 8

php – upload and convert csv UTF16 to utf 8

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m looking to upload a csv file encoded in UTF-16 on my server and convert it to UTF-8. I managed to encode the parse of the file but I cannot convert the data to UTF-8.

i’ve read some posts on stack overflow about convert utf16 to ut8 but nothing works

below my script

    <?php

// here upload file


// here convert file

$row = 1;
if (($handle = fopen("export.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
        $num = count($data);


        if ($row > 1) {


            $emballage = strtoupper((trim(str_replace('"', '', $data[4]))));
            $lot = strtoupper((trim(str_replace('"', '', $data[5]))));
            $peremption = strtoupper((trim(str_replace('"', '', $data[8]))));
            $peremption_reformat = implode("-", array_reverse(explode("/", $peremption)));

            $code_produit = trim(str_replace('"', '', $data[1]));
            $code_produit = intval($code_produit);

            if ($code_produit == 142935) {
                $id_equipment_model = 22; //b15

            } elseif ($code_produit == 142931) {
                $id_equipment_model = 23; //B5

            } else {
                $id_equipment_model = 21; //B2

            }


            $sql_insert = 'INSERT INTO bt_airpoduct_auto_evol2 (emballage, lot, peremption, id_equipment_model)
    VALUES ("' . $emballage . '", "' . $lot . '", "' . $peremption_reformat . '", "' . $id_equipment_model . '")';


            $stmt = $conn->executeQuery($sql_insert);


        }
        $row++;


    }
    fclose($handle);
}
?>
How to&Answers: