读取csv文件数量不完全难点,读取csv文件遭遇的乱码难点

标题分析出来的数码不完全,有为空的字段
互连网查了下说是在php5.2.8 中设有bug
消除办法是运用自定义函数

在windows其余版本中

看样子有人在有汉字的字符串 前加三个 ‘
或是放肆半角符号,让bug将其除掉,也就那样做太费事了。最终吧,找来叁个仿照fgetcsv效能的函数。

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] && !$data[2]) break;
}
fclose($handle);
?>

 代码如下

 代码如下

复制代码 代码如下:

读取csv文件数量不完全难点,读取csv文件遭遇的乱码难点。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_读取csv文件数量不完全难点,读取csv文件遭遇的乱码难点。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);

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

一、使用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)
             $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’ , $_www.5929.com,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’);

自己选取了首个章程,确实可行,第4个措施没有测量检验

www.5929.com 1

Leave a Comment.