Encoding problems with CSV export from Excel
<p>I have an encoding problem with Microsoft Excel CSV exports. I created an import method which reads a CSV file containing contacts to push them to our database.</p>

<p>Here is my process </p>

<ol>
<li><p>When I <code>save as</code> from Microsoft Excel to create a CSV file which contains unicode characters like <code>è</code>, I have my generated CSV file.</p></li>
<li><p>When I upload it through POSTMAN and then I read the content of the temp file with <code>fread</code>, <code>file</code> or <code>file_get_contents</code>, I have some bad characters.</p></li>
</ol>

<p>When I use LibreOffice (<a href="https://fr.libreoffice.org/" rel="nofollow noreferrer">https://fr.libreoffice.org/</a>), all is good and I have into my PHP code the good encoding.</p>

<p>I tried many solutions as far as I could do like <code>mb_convert_encoding</code>, <code>utf8_encode</code>, <code>iconv</code> but nothing would work.</p>

<p>That's what I get from my var_dump</p>

<p><a href="https://i.stack.imgur.com/hJJ9a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hJJ9a.png" alt="Var_dump"></a></p>

<p>That's my code which converts the file's content into an array to iterate over the lines in order to convert a line into an array.</p>

<pre><code>$iterableContacts = Closure::bind(function () use ($filePath) {
    $keys = [
        'email',
        'language',
        'lastname',
        'firstname',
        'organization',
        'job',
        'street',
        'street_complement',
        'zipcode',
        'city',
        'country',
        'note'
    ];

    // var_dump(file($filePath));

    foreach (file($filePath, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES) as $key =&gt; $line) {
        $line = str_getcsv($line, ';');

        if (trim(strtolower($line[0])) == 'email') {
            continue;
        } elseif (count($line) === count($keys) &amp;&amp; !empty($line)) {
            yield ($key - 1) =&gt; array_combine($keys, array_values($line));
        }
    }
}, null);
</code></pre>

<p><code>mb_detect_encoding</code> returns me <code>ASCII</code></p>

<p>Anyone can help me please?</p>

<p>Microsoft Excel 2016 build 15.26 (160910)
PHP 5.6.28-1</p>