读取csv文件数量不完整难点,中的数据不完整

by admin on 2019年7月26日

看来有人在有汉字的字符串 前加几个 ‘
或是跋扈半角符号,让bug将其除掉,不过尔尔做太辛苦了。最终吧,找来贰个模拟fgetcsv效用的函数。

难点分析出来的数额不完全,有为空的字段
英特网查了下说是在php5.2.8 中存在bug
解决办法是利用自定义函数

在windows其余版本中

1、明天一项目需求经过上传CSV文件是想批量导入,于是利用了fgetcsv()
函数,函数代码如下:
<?php
$row = 1;
$handle = fopen(“ceshi_1.csv”,”r”);
while ($data = fgetcsv($handle, 1000, “,”)) {
    //$num = count($data);
$num = 29;
    echo “<p> $num fields in line $row: <br>\n”;
    $row++;
    for ($c=0; $c < $num; $c++) {
   $data[$c]=iconv(“gbk”, “utf-8//IGNORE”,$data[$c]); 
        echo $data[$c] . “<br>\n”;
    }
if (!$data[0] && !$data[1]读取csv文件数量不完整难点,中的数据不完整。 && !$data[2]) break;
}
fclose($handle);
?>

复制代码 代码如下:

 代码如下

 代码如下

2、上边代码在该地质度量试一点都没难题,但传播服务器(Linux)上则相会世汉字乱码难题,花了许久时日,在互连网终于找到了消除办法,大致有二种:

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文件数量不完整难点,中的数据不完整。csv_i]);
}
return empty ($_line) ? false : $_csv_data;
}

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

# 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);

一、使用PHP自定义管理.csv文件的函数:

其一代码未有其他难点,然后作者放到了linux中窥见有为空的字段了。

/**
* 对CSV举办拍卖
* @param resource handle
* @param int length
* @param string delimiter
* @param string enclosure
* @return 文件内容或FALSE。
*/
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;
}

主题材料分析出来的数目不完整,有为空的字段
网络查了下说是在php5.2.8 中存在bug
消除办法是采用自定义函数

二、使用: setlocale 函数设置境况变量。:

 代码如下

设置setlocale(LC_ALL,’zh_CN’);

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)
bf88必发唯一官网 ,             $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;
}

做项目涉及到复杂的文书档案,就满门简体繁体都丰盛,应该影响不会异常的大:setlocale(LC_ALL,’zh_CN’);

本人采取了第一个格局,确实可行,第四个格局未有测验

bf88必发唯一官网 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图