Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:56:22+00:00 2026-06-18T22:56:22+00:00

Based on the answer provided here by user user1830391: Some characters in CSV file

  • 0

Based on the answer provided here by user user1830391:
Some characters in CSV file are not read during PHP fgetcsv()

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

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.

function runCSVtoArray() {
    // --> FOR IMPORT
    //Function that converts a CSV file to a PHP array.
    //echo '<span class="success">Chargement du fichier CSV pour importation MYSQL....</span><br />';
    $readCharsPerLine = (JRequest::getVar('charsPerLine') > 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->imp['importPath'].$this->imp['csvFileName'];
    if ( file_exists($openfile) ) {
        //echo '<span class="success">Fichier CSV trouvé....</span><br />';
        //echo '<span class="success">Ouverture du fichier : '.$openfile.'</span><br />';
        if (($handle = fopen($openfile, "r")) !== FALSE) {
            //echo '<span class="success">Fichier CSV ouvert... Chargement en cours....</span><br />';
            $row_i=0;
            $this->_importData = array();
            /*while (($data = fgetcsv($handle, $readCharsPerLine, ";")) !== FALSE) {*/
            while (($the_line = fgets($handle)) !== FALSE) {
                $data = explode(';', $the_line);
                $debugoutput = implode('; ', $data).'<br />'; echo ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($debugoutput, "UTF-8") == "UTF-8") ) ? utf8_encode($debugoutput) : $debugoutput.'<br />'; //Debug2
                /*
                $num        = count($data);
                if ($row_i==0) {
                    // TITLE ROW
                    $keyRow = array();
                    for ($c=0; $c < $num; $c++) {
                        //Making title array with CSV first line
                        //Key for colum
                        if ( (JRequest::getVar('encodeutf8')) && ( 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 < $num; $c++) {
                        $key = $keyRow[$c];
                        if ( (JRequest::getVar('encodeutf8')) && ( 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->_importData[$row_i][$key]=trim($data[$c]);
                            $this->_importData[$row_i][$key]=str_replace('GDWACCENT', '', $this->_importData[$row_i][$key]);    //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                        }
                    }
                }
                */
                $row_i++;
            } //End while()
            //echo '<span class="success">Chargement terminer.... Sauvegarde en cours...</span><br />';
            return true;
        } else {
            //Incapable d'ouvrir le fichier d'importation.
            return false;
        }
    } else {
        //FILE NOT FOUND...
        return false;
    }
} // runCSVtoArray()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:56:23+00:00Added an answer on June 18, 2026 at 10:56 pm

    I solved this by opening the file using fopen and fgets instead of fgetcsv() and writing a copy using utf8_encode for each line. Then i use the copy and put that through fgetcsv()

    here is my updated code.

    function runCSVtoArray() {
        // --> FOR IMPORT
        //Function that converts a CSV file to a PHP array.
        //echo '<span class="success">Chargement du fichier CSV pour importation MYSQL....</span><br />';
        $readCharsPerLine = (JRequest::getVar('charsPerLine') > 0) ? JRequest::getVar('charsPerLine') : 1500; /* Import as of 2012-04-16 seem to have max 800chars per line. 1500 is alot of extra. */
        putenv("LANG=fr_CA.UTF-8");
        setlocale(LC_ALL, 'fr_CA.UTF-8');
        //ini_set("auto_detect_line_endings", true);
        //iconv_set_encoding("internal_encoding", "UTF-8");
        $openfile = $this->imp['importPath'].$this->imp['csvFileName'];
        $utf8File = str_replace('.csv', '_utf8.csv', $openfile);
    
        if ( file_exists($openfile) ) {
            //echo '<span class="success">Fichier CSV trouvé....</span><br />';
    
            //rewrite the file in UTF8
            if (JRequest::getVar('encodeutf8')) {
                if (($handle = fopen($openfile, "r")) !== FALSE) {
                    $newFileHandle = fopen($utf8File, 'w');     //NEW UTF8 FORMAT
                    //fwrite($newFileHandle, "\xEF\xBB\xBF");
                    while (($the_line = fgets($handle)) !== FALSE) {
                        fwrite($newFileHandle, utf8_encode($the_line));
                    }   //End of while()
                }
                $openfile = $utf8File;
            }
    
            //echo '<span class="success">Ouverture du fichier : '.$openfile.'</span><br />';
            if (($handle = fopen($openfile, "r")) !== FALSE) {
                //echo '<span class="success">Fichier CSV ouvert... Chargement en cours....</span><br />';
                $row_i=0;
                $this->_importData = array();
                while (($data = fgetcsv($handle, $readCharsPerLine, ";")) !== FALSE) {
                /*while (($the_line = fgets($handle)) !== FALSE) {*/
                    //$data = explode(';', $the_line);
                    //$debugoutput = implode('; ', $data); echo ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($debugoutput, "UTF-8") == "UTF-8") ) ? utf8_encode($debugoutput).'<br />' : $debugoutput.'<br />';   //Debug2
                    //$debugoutput = implode('; ', $data); echo $debugoutput.'<br />';  //Debug2
                    $num            = count($data);
                    if ($row_i==0) {
                        // TITLE ROW
                        $keyRow = array();
                        $maxItems = count($data);   //Count the number of ";"
                        for ($c=0; $c < $num; $c++) {
                            //Making title array with CSV first line
                            //Key for colum
                            if ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) {
                                //$data[$c] = utf8_encode($data[$c]);
                                $data[$c] = $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 < $num; $c++) {
                            $key = $keyRow[$c];
                            if ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) {
                                //$data[$c] = utf8_encode($data[$c]);
                                $data[$c] = $data[$c];
                                $data[$c]=str_replace('GDWACCENT', '', $data[$c]);  //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                            }
                            if ($data[$c]!="") {
                                $this->_importData[$row_i][$key]=trim($data[$c]);
                                $this->_importData[$row_i][$key]=str_replace('GDWACCENT', '', $this->_importData[$row_i][$key]);    //STRIP GDWACCENT, GDW uTF8 fgetcsv fix
                            }
                        }   //End of for()
                    }
                    $row_i++;
                } //End while()
                //echo 'HERE<br />';
                //gdwprint($this->_importData);
                //exit();
                //echo '<span class="success">Chargement terminer.... Sauvegarde en cours...</span><br />';
                return true;
            } else {
                //Incapable d'ouvrir le fichier d'importation.
                return false;
            }
        } else {
            //FILE NOT FOUND...
            return false;
        }
    } // runCSVtoArray()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on the answer provided here , I am attempting to validate whether or
Here is a perplexing issue I have not seen a good answer to on
SOLUTION Refer to my answer below: issues with form/iframe based file upload in Opera
I'm running a php server based on the one provided by a tutorial by
I read some of the answers on here re: testing views and controllers, and
I am trying to evaluate the answer provided here , and am getting the
Based on an answer from a candidate I have a confusion regarding the functioning
Based on the answer for this question What's the difference between CompositionBatch and catalogs?
Based the accepted answer to this question I've setup a NetBeans/tomcat environment. In testing
Based on one answer to an earlier post , I'm investigating the possibility of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.