PHP fgetcsv() not reading first UTF-8 caracter - alternatif using fgets
<p>Based on the answer provided here by user user1830391:
<a href="http://stackoverflow.com/questions/1472886/some-characters-in-csv-file-are-not-read-during-php-fgetcsv">Some characters in CSV file are not read during PHP fgetcsv()</a></p>

<p>I updated my following code to use fgets() instead of fgetcsv(). It fixed my first character issue. thats no longer a prob... but...</p>

<p>what if the .csv file is seprated using ; instead of , Some fields will be wrapped using double quotes "", for example one of my rows is split onto 2 lines. quote opened in the last element of one line and closed at the end of the first element of the next line. There is an "enter"(/n) in that cell. how should i treat this using this code. fgetcsv catches elements within double quotes but i dont think fgets() does.</p>

<pre><code>function runCSVtoArray() {
    // --&gt; FOR IMPORT
    //Function that converts a CSV file to a PHP array.
    //echo '&lt;span class="success"&gt;Chargement du fichier CSV pour importation MYSQL....&lt;/span&gt;&lt;br /&gt;';
    $readCharsPerLine = (JRequest::getVar('charsPerLine') &gt; 0) ? JRequest::getVar('charsPerLine') : 1500; /* Import as of 2012-04-16 seem to have max 800chars per line. 1500 is alot of extra. */
    ini_set("auto_detect_line_endings", true);
    iconv_set_encoding("internal_encoding", "UTF-8");
    $openfile = $this-&gt;imp['importPath'].$this-&gt;imp['csvFileName'];
    if ( file_exists($openfile) ) {
        //echo '&lt;span class="success"&gt;Fichier CSV trouvé....&lt;/span&gt;&lt;br /&gt;';
        //echo '&lt;span class="success"&gt;Ouverture du fichier : '.$openfile.'&lt;/span&gt;&lt;br /&gt;';
        if (($handle = fopen($openfile, "r")) !== FALSE) {
            //echo '&lt;span class="success"&gt;Fichier CSV ouvert... Chargement en cours....&lt;/span&gt;&lt;br /&gt;';
            $row_i=0;
            $this-&gt;_importData = array();
            /*while (($data = fgetcsv($handle, $readCharsPerLine, ";")) !== FALSE) {*/
            while (($the_line = fgets($handle)) !== FALSE) {
                $data = explode(';', $the_line);
                $debugoutput = implode('; ', $data).'&lt;br /&gt;'; echo ( (JRequest::getVar('encodeutf8')) &amp;&amp; ( mb_detect_encoding($debugoutput, "UTF-8") == "UTF-8") ) ? utf8_encode($debugoutput) : $debugoutput.'&lt;br /&gt;'; //Debug2
                /*
                $num        = count($data);
                if ($row_i==0) {
                    // TITLE ROW
                    $keyRow = array();
                    for ($c=0; $c &lt; $num; $c++) {
                        //Making title array with CSV first line
                        //Key for colum
                        if ( (JRequest::getVar('encodeutf8')) &amp;&amp; ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) { $data[$c] = utf8_encode($data[$c]); }
                        if ($data[$c]!="") {
                            $keyRow[$c]=trim($data[$c]);
                            $keyRow[$c]=str_replace('GDWACCENT', '', $keyRow[$c]);  //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                        }
                        else { $keyRow[$c]=''; }
                    }
                } else {
                    //VALUE ROW...
                    for ($c=0; $c &lt; $num; $c++) {
                        $key = $keyRow[$c];
                        if ( (JRequest::getVar('encodeutf8')) &amp;&amp; ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) {
                            $data[$c] = utf8_encode($data[$c]);
                            $data[$c]=str_replace('GDWACCENT', '', $data[$c]);  //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                        }
                        if ($data[$c]!="") {
                            $this-&gt;_importData[$row_i][$key]=trim($data[$c]);
                            $this-&gt;_importData[$row_i][$key]=str_replace('GDWACCENT', '', $this-&gt;_importData[$row_i][$key]);    //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                        }
                    }
                }
                */
                $row_i++;
            } //End while()
            //echo '&lt;span class="success"&gt;Chargement terminer.... Sauvegarde en cours...&lt;/span&gt;&lt;br /&gt;';
            return true;
        } else {
            //Incapable d'ouvrir le fichier d'importation.
            return false;
        }
    } else {
        //FILE NOT FOUND...
        return false;
    }
} // runCSVtoArray()
</code></pre>
