Home Backend Development PHP Tutorial Solve the problem of incomplete data in csv files read by php fgetcsv_PHP tutorial

Solve the problem of incomplete data in csv files read by php fgetcsv_PHP tutorial

Jul 13, 2016 am 10:43 AM
csv php No whole data document solve read question

The csv file is read by the fgetcsv function in php, but in the php5.2.8 version of Linux, it will be found that the csv file data read by fgetcsv is incomplete. Let's look at the solution to the problem.

In other versions of windows

                              //print_r($data);
The code is as follows
 代码如下 复制代码

# Open the File.
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $nn = 0;
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
       
//print_r($data);
        # Count the total keys in the row.
        $c = count($data);
        # Populate the multidimensional array.
        for ($x=0;$x<$c;$x++)
        {
            $csvarray[$nn][$x] = $data[$x];
        }
        $nn++;
    }
    # Close the File.
    fclose($handle);
}
//print_r($csvarray);

Copy code

# Open the File.
if (($handle = fopen("test.csv", "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.

$nn = 0;
 代码如下 复制代码

function __fgetcsv(& $handle, $length = null, $d = ',', $e = '"') {
     $d = preg_quote($d);
     $e = preg_quote($e);
     $_line = "";
     $eof=false;
     while ($eof != true) {
         $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
         $itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
         if ($itemcnt % 2 == 0)
             $eof = true;
     }
     $_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
     $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
     preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
     $_csv_data = $_csv_matches[1];
     for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
         $_csv_data[$_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '' , $_csv_data[$_csv_i]);
         $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
     }
     return empty ($_line) ? false : $_csv_data;
}

While (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
            # Count the total keys in the row.           $c = count($data);

​​​​ # Populate the multidimensional array.

for ($x=0;$x<$c;$x++)           }           $nn++; } # Close the File. fclose($handle); } //print_r($csvarray);
There is no problem with this code, but then I put it in Linux and found that there were empty fields. The data parsed by the problem is incomplete and there are empty fields
I checked online and said there is a bug in php5.2.8
The solution is to use a custom function
The code is as follows Copy code
function __fgetcsv(& $handle, $length = null, $d = ',', $e = '"') { $d = preg_quote($d); $e = preg_quote($e); $_line = ""; $eof=false; While ($eof != true) {              $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));              $itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);               if ($itemcnt % 2 == 0)                 $eof = true; }   $_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line)); $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/'; Preg_match_all($_csv_pattern, $_csv_line, $_csv_matches); $_csv_data = $_csv_matches[1]; for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {              $_csv_data[$_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '$1' , $_csv_data[$_csv_i]);            $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]); }   Return empty ($_line) ? false : $_csv_data; } http://www.bkjia.com/PHPjc/633189.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633189.htmlTechArticleThe csv file is read by the fgetcsv function in php, but it will be read in the php5.2.8 version of Linux. It is found that the csv file data read by fgetcsv is incomplete. Let's look at the solution to the problem. In wi...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles