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
The code is as follows
|
Copy code
|
||||
# Open the File.
|
# Populate the multidimensional array.
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 fieldsThe 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... |