php - 将数据放入.csv文件中

时间:2018-01-27 16:30:44

标签: php csv fputcsv

我试图使用PHP将一些数据放入.csv文件中,我可以把它放在文件中,但问题是双引号弄乱了我。这是我必须在.csv文件中添加的文本类型:

#2500
Pittsburgh Penguins 3 , Montreal Canadiens 5
Shots: 28 - 25
PIT PP: 1 of 3 (4 shots), Passing:65/82=79 %, Challenges:31/38=79 %
MTL PP: 0 of 3 (7 shots), Passing:66/84=78 %, Challenges:42/50=82 %
Attendance: 6839 of 18387 (PPG Paints Arena)
Ticket income: 317330 Match televised: No


Pittsburgh Penguins scoring.
                         ( G  A  Sh Hi Ga Ta PIM, +/-  Icetime Rating)
R Ryan Reaves             0  0  0  0  1  0    0    0    7.40    5
R Bryan Rust              0  0  0  0  2  0    0   -1   11.50    5
R Carl Hagelin            0  0  3  0  0  0    0   -1   14.50    6
R Patric Hörnqvist        1  0  3  6  0  0    2   -1   20.40    9
C Michael Raffl           0  0  2  2  0  1    0    0    9.00    7
C Riley Sheahan           0  0  0  2  0  1    0   -1   17.10    7
C Evgeni Malkin           1  1  3  0  0  0    0    0   16.20    7
C Sidney Crosby           0  1  3  1  0  0    0   -2   17.30    7
L Greg McKegg             0  0  3  3  2  0    2    0    8.50    6
L Tom Kuhnhackl           0  0  0  0  1  0    0   -1   11.50    5
L Jake Guentzel           1  1  4  0  0  0    2    0   20.10    8
L Conor Sheary            0  0  2  0  0  0    0   -2   17.30    6
D Jamie Oleksiak          0  0  1  1  0  1    0   -1   13.00    7
D Olli Määttä             0  1  1  1  0  0    0   -1   13.40    6
D John Carlson            0  0  1  0  0  0    0   -1   21.40    7
D Brian Dumoulin          0  0  0  1  0  3    0   -1   19.00    6
D Justin Schultz          0  0  3  0  0  0    0   -1   18.30    7
D Kris Letang             0  1  1  2  0  1    0   -1   26.20    7
                         (GA,  A, Sv , PIM, +/- Icetime Rating)
G Matthew Murray          5   0   20   0    0   59.00    4
G Tristan Jarry           0   0   0    0    0    0.00    0

Montreal Canadiens scoring.
                         ( G  A  Sh Hi Ga Ta PIM, +/-  Icetime Rating)
R Derek Dorsett           0  0  0  2  2  2    0   -1   14.00    6
R Andrew Shaw             1  0  2  1  1  1    0    1   14.10    7
R Brendan Gallagher       0  1  1  0  0  0    2    2   11.50    7
R Max Pacioretty          0  1  3  2  1  0    0    1   20.40    7
C Michael McCarron        0  0  1  1  1  0    2   -1   11.00    6
C Paul Byron              0  2  2  0  0  0    0    1   19.20    8
C Phillip Danault         1  0  2  0  0  0    0    2   14.10    7
C Jonathan Drouin         1  0  8  0  0  1    0    1   20.40    9
L Nicolas Deslauriers     0  0  2  1  0  0    2   -1   13.20    7
L Artturi Lehkonen        0  0  2  0  0  0    0    1   11.50    7
L Charles Hudon           1  1  3  0  1  0    0    2   13.30    9
L Alex Galchenyuk         1  0  3  2  1  0    0    1   20.40    8
D David Schlemko          0  0  0  0  0  0    0    1   11.50    6
D Jeff Petry              0  0  0  2  0  2    0    1   17.20    7
D Shea Weber              0  0  0  0  0  0    0    1   30.10    7
D Jordie Benn             0  1  0  1  1  1    0    1   13.10    6
D Karl Alzner             0  2  0  0  0  0    0    2   14.00    7
D Shea Theodore           0  2  1  0  0  0    0    0   26.00    9
                         (GA,  A, Sv , PIM, +/- Icetime Rating)
G Carey Price             3   0   25   0    0   60.00    7
G Antti Niemi             0   0   0    0    0    0.00    0

Three stars of the game:
1st star: Charles Hudon
2nd star: Shea Theodore
3rd star: Andrew Shaw

Notes for Pittsburgh Penguins:

Notes for Montreal Canadiens:

Other notes:


###

这里重要的事情是'#'这允许我分隔ma文本放入数组。这是我的代码:

$gameChange = "###";
$i = 0;
$reportList = array();
$allReports = array();
$lineStr = null;

$reportsFile = fopen("reports.ehm", "r");
if ($reportsFile) {
    while (($line = fgets($reportsFile)) !== false) {
        if ($i == 0) {
            array_push($reportList, trim(str_replace("#","",$line)));
        }
        if (trim($line) == $gameChange) {
            array_push($reportList, $lineStr));
            array_push($allReports, $reportList);
            $reportList = array();
            $lineStr = "";
            $i = 0;
        } else {
            if ($i != 0) {
                $lineStr = $lineStr.$line;
            }
            $i += 1;
        }
    }

    fclose($reportsFile);

    $reportsFileCSV = fopen("reports.csv", "w");
    foreach ($allReports as $report) {
        fputcsv($reportsFileCSV, [
            $report[0],
            $report[1],
        ],'|');
    }
    fclose($reportsFileCSV);
} else {
    echo "ERROR!";
}

逻辑很好,它会产生一个像样的.csv文件,但看起来当我 fputcsv 时,它会保留双引号。

我已尝试str_replace('"', '', $line)并使用修剪功能,但没有结果。

以下是.csv文件中数据的结果:

2500|"Pittsburgh Penguins 3 , Montreal Canadiens 5
Shots: 28 - 25
PIT PP: 1 of 3 (4 shots), Passing:65/82=79 %, Challenges:31/38=79 %
MTL PP: 0 of 3 (7 shots), Passing:66/84=78 %, Challenges:42/50=82 %
Attendance: 6839 of 18387 (PPG Paints Arena)
Ticket income: 317330 Match televised: No


Pittsburgh Penguins scoring.
                         ( G  A  Sh Hi Ga Ta PIM, +/-  Icetime Rating)
R Ryan Reaves             0  0  0  0  1  0    0    0    7.40    5
R Bryan Rust              0  0  0  0  2  0    0   -1   11.50    5
R Carl Hagelin            0  0  3  0  0  0    0   -1   14.50    6
R Patric Hörnqvist        1  0  3  6  0  0    2   -1   20.40    9
C Michael Raffl           0  0  2  2  0  1    0    0    9.00    7
C Riley Sheahan           0  0  0  2  0  1    0   -1   17.10    7
C Evgeni Malkin           1  1  3  0  0  0    0    0   16.20    7
C Sidney Crosby           0  1  3  1  0  0    0   -2   17.30    7
L Greg McKegg             0  0  3  3  2  0    2    0    8.50    6
L Tom Kuhnhackl           0  0  0  0  1  0    0   -1   11.50    5
L Jake Guentzel           1  1  4  0  0  0    2    0   20.10    8
L Conor Sheary            0  0  2  0  0  0    0   -2   17.30    6
D Jamie Oleksiak          0  0  1  1  0  1    0   -1   13.00    7
D Olli Määttä             0  1  1  1  0  0    0   -1   13.40    6
D John Carlson            0  0  1  0  0  0    0   -1   21.40    7
D Brian Dumoulin          0  0  0  1  0  3    0   -1   19.00    6
D Justin Schultz          0  0  3  0  0  0    0   -1   18.30    7
D Kris Letang             0  1  1  2  0  1    0   -1   26.20    7
                         (GA,  A, Sv , PIM, +/- Icetime Rating)
G Matthew Murray          5   0   20   0    0   59.00    4
G Tristan Jarry           0   0   0    0    0    0.00    0

Montreal Canadiens scoring.
                         ( G  A  Sh Hi Ga Ta PIM, +/-  Icetime Rating)
R Derek Dorsett           0  0  0  2  2  2    0   -1   14.00    6
R Andrew Shaw             1  0  2  1  1  1    0    1   14.10    7
R Brendan Gallagher       0  1  1  0  0  0    2    2   11.50    7
R Max Pacioretty          0  1  3  2  1  0    0    1   20.40    7
C Michael McCarron        0  0  1  1  1  0    2   -1   11.00    6
C Paul Byron              0  2  2  0  0  0    0    1   19.20    8
C Phillip Danault         1  0  2  0  0  0    0    2   14.10    7
C Jonathan Drouin         1  0  8  0  0  1    0    1   20.40    9
L Nicolas Deslauriers     0  0  2  1  0  0    2   -1   13.20    7
L Artturi Lehkonen        0  0  2  0  0  0    0    1   11.50    7
L Charles Hudon           1  1  3  0  1  0    0    2   13.30    9
L Alex Galchenyuk         1  0  3  2  1  0    0    1   20.40    8
D David Schlemko          0  0  0  0  0  0    0    1   11.50    6
D Jeff Petry              0  0  0  2  0  2    0    1   17.20    7
D Shea Weber              0  0  0  0  0  0    0    1   30.10    7
D Jordie Benn             0  1  0  1  1  1    0    1   13.10    6
D Karl Alzner             0  2  0  0  0  0    0    2   14.00    7
D Shea Theodore           0  2  1  0  0  0    0    0   26.00    9
                         (GA,  A, Sv , PIM, +/- Icetime Rating)
G Carey Price             3   0   25   0    0   60.00    7
G Antti Niemi             0   0   0    0    0    0.00    0

Three stars of the game:
1st star: Charles Hudon
2nd star: Shea Theodore
3rd star: Andrew Shaw

Notes for Pittsburgh Penguins:

Notes for Montreal Canadiens:

Other notes:


"

似乎在字符串的开头和结尾都放了一个行分隔符,所以当我尝试将它放在DB中时,没有数据。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

fputcsv()的第四个参数是封闭字符。

<?php

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields, "|", " ");
}

fclose($fp);
?>

但是,这不是csv文件。