PHP fgetcsv() 分隔符 ';' 未能识别

Zha*_*ang 7 php csv

看来我对“;”有问题 分隔符。这是我的 csv 文件:

First Name;Last Name;Email;Age
Julie;Brown;julie@example.com;52
Dan;Wong;dan@example.com;19
Tim;Hortons;tim@example.com;27
Run Code Online (Sandbox Code Playgroud)

和我的 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);
}
Run Code Online (Sandbox Code Playgroud)

我有这个:

1 champs à la ligne 1: 
First Name;Last Name;Email;Age
1 champs à la ligne 2: 
Julie;Brown;julie@example.com;52
1 champs à la ligne 3: 
Dan;Wong;dan@example.com;19
1 champs à la ligne 4: 
Tim;Hortons;tim@example.com;27
Run Code Online (Sandbox Code Playgroud)

当我使用“,”分隔符时,而不是像这样

4 champs à la ligne 1: 
First Name
Last Name
Email
Age
Run Code Online (Sandbox Code Playgroud)

此外,我想知道是否可以有各种分隔符。因为我想显示用户上传的 csv 文件,我不想强​​迫他们使用一个预先确定的分隔符。

谢谢

fyr*_*rye 6

第二个参数是长度,所以你的 fgetcsv 应该是

fgetcsv($handle, 0, ';');
Run Code Online (Sandbox Code Playgroud)

导致

4 champs à la ligne 1: 

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

Julie
Brown
julie@example.com
52
4 champs à la ligne 3: 

Dan
Wong
dan@example.com
19
4 champs à la ligne 4: 

Tim
Hortons
tim@example.com
27
Run Code Online (Sandbox Code Playgroud)

至于你关于变量分隔符的第二个问题。到目前为止,最简单的方法是允许用户定义在上传表单上使用哪个分隔符,可能使用可接受分隔符的选择元素,然后在读取 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'];
Run Code Online (Sandbox Code Playgroud)

您还可以解析检查所需分隔符的行。

$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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)