2016-06-10 125 views
3

看來我有一個問題,「;」分隔符。這是我的csv文件:PHP fgetcsv()分隔符';'不認可

First Name;Last Name;Email;Age 
Julie;Brown;[email protected];52 
Dan;Wong;[email protected];19 
Tim;Hortons;[email protected];27 

和我的PHP代碼:

$row = 1; 
if (($handle = fopen("upload/".$_FILES['fichier']['name'], "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, ";")) !== FALSE) { 
     $num = count($data); 
     echo "<p> $num champs à la ligne $row: <br /></p>\n"; 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      echo $data[$c] . "<br />\n"; 
     } 
    } 
fclose($handle); 
} 

,我有這一點的說:

1 champs à la ligne 1: 
First Name;Last Name;Email;Age 
1 champs à la ligne 2: 
Julie;Brown;[email protected];52 
1 champs à la ligne 3: 
Dan;Wong;[email protected];19 
1 champs à la ligne 4: 
Tim;Hortons;[email protected];27 

,而不是像這樣,當我使用 ''定界符

4 champs à la ligne 1: 
First Name 
Last Name 
Email 
Age 

另外,我想知道是否有可能有不同的分隔符。因爲我想顯示用戶上傳的csv文件,我不想強​​迫他們使用一個預定義的分隔符。

由於

+0

更新了我的答案,其中第二個問題的例子是使用用戶定義的多個分隔符或逐行解析CSV數據。 – fyrye

回答

2

第二個參數是長度,所以你fgetcsv應該是

fgetcsv($handle, 0, ';'); 

4 champs à la ligne 1: 

First Name 
Last Name 
Email 
Age 
4 champs à la ligne 2: 

Julie 
Brown 
[email protected] 
52 
4 champs à la ligne 3: 

Dan 
Wong 
[email protected] 
19 
4 champs à la ligne 4: 

Tim 
Hortons 
[email protected] 
27 

至於你上可變的分隔符的第二個問題得到的。到目前爲止,最簡單的方法將允許用戶定義在上載表單上使用哪個分隔符,可能使用可接受的分隔符的select元素,然後在讀取csv時使用它。

對於實施例

$allowedDelimiters = [',', ';']; 
$defaultDelimiter = ';'; 
if (true === empty($_POST['delimiter'])) { 
    $_POST['delimiter'] = $defaultDelimiter; 
} 
if (!in_array($_POST['delimiter'], $allowedDelimiters, true)) { 
    $_POST['delimiter'] = $defaultDelimiter; 
    //alternatively redirect back to the form with an error message 
} 
$delimiter = $_POST['delimiter']; 

也可以解析該線檢查所需的分隔符。

$filename = "upload/".$_FILES['fichier']['name']; 
if (($handle = fopen($filename, "r")) !== false) { 
    $content = fread($handle, filesize($filename)); 
    fclose($handle); 
    //count the delimiters 
    $semiColons = substr_count($content, ';'); 
    $commas = substr_count($content, ','); 
    //read each row 
    $rows = str_getcsv($content, "\n"); 
    $rowCount = count($rows); 
    $delimiter = null; 
    foreach ($rows as $line => $row) { 
     //check the delimiters 
     if (false === isset($delimiter)) { 
      /* 
       determine if the delimiter total divided by the number 
       of rows matches the delimiters found on this row 
       and use it to parse the columns 
      */ 
      if ($semiColons > 0 && $semiColons/$rowCount === substr_count($row, ';')) { 
       $delimiter = ';'; 
      } elseif ($commas > 0 && $commas/$rowCount === substr_count($row, ',')) { 
       $delimiter = ','; 
      } 
     } 
     //read the columns using the detected delimiter 
     //otherwise use a default if a delimiter could not be determined 
     $columns = str_getcsv($row, $delimiter ? : ';'); 
     echo "<p>$rowCount champs à la ligne " . ($line + 1) . "</p>\n"; 
     foreach ($columns as $column) { 
      echo $column . "<br/>\n"; 
     } 
    } 
} 
2

陣列fgetcsv(資源$處理[摘要$長度= 0 [,字符串$分界= 「」[,字符串$外殼= '「'[,字符串$逃逸=」\ 「]]]])

Manual

第二個參數是長度。

fgetcsv($handle, 0, ";") 
+0

哦,非常感謝。你有第二個問題的解決方案嗎?例如有人用';'上傳csv文件使用','分隔符和另一個分隔符如何處理這2個案例?和其他分隔符的情況 – Zhang

+0

閱讀第一行並搜索';'或','並在不使用'fgetcsv'並使用它並逐行讀取文件並使用'preg_split('/ [; | ] /',$ line)'。 –

+0

謝謝,我會看看:) – Zhang