Home > Backend Development > PHP Tutorial > The data of fgetcsv function in php is incomplete in php5.2.8_PHP tutorial

The data of fgetcsv function in php is incomplete in php5.2.8_PHP tutorial

WBOY
Release: 2016-07-13 10:43:12
Original
1101 people have browsed it

This article will introduce to you the solution to the incomplete data problem of fgetcsv function in php in php5.2.8. I hope this article will be helpful to you.

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
 代码如下 复制代码

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;
}

Copy code
function __fgetcsv(& $handle, $length = null, $d = ',', $e = '"') { $d = preg_quote($d);

$e = preg_quote($e);

$_line = ""; 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/633199.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633199.htmlTechArticleThis article will introduce to you how to solve the problem of incomplete data in php5.2.8 using the fgetcsv function in php. Method, I hope this article will be helpful to all my friends. The problem is analyzed...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template