解析csv,其中最后一个“标题”后跟一个空格

时间:2017-06-07 13:49:06

标签: php csv parsing

我正在尝试使用str_getcsv以下列方式解析csv但是我遇到了两个问题(如下所示)。

$url = 'http://my.events.calendar/api/csv?mycalendarquery';
$response = wp_remote_get($url);
$response_body = $response['body']; // this is a really long string
$parsed_string = str_getcsv($response_body, ',', '"');
$full_events_array = array_chunk($parsed_string, 11);

问题1 - 我无法使用explode,因为csv中的一个字段是"Description",其中包含许多/冗长的事件描述。当然,这些包括逗号,新行,回报等......

问题2 - 这是我有疑问的问题。 csv文件的类别(标题?)是"Subject", "Date", (more things here...) "Description", "Calendar Address"。但是最后一个之后没有逗号。因此,"Subject""Calendar"地址的条目正如此组合 -

array(11) {
  [0] =>
  string(32) "Calendar Address
  "Application Due"" // this pattern happens for every event but with a url instead.
// Yes, I know this looks wrong in the code block, but this is exactly how the data is coming in (complete with the double quotes).

如何解析此问题,以便Calendar AddressApplication Due分开?

作为参考,我还尝试了str_getcsv($response_body, '"', ','); str_getcsv($response_body, ',', '"', '\n');和其他几种组合。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

此脚本将CSV文件读取为251行,每行12个元素。

<?php // demo/temp_44414553.php
/**
 * https://stackoverflow.com/questions/44414553/parsing-csv-where-the-last-header-is-followed-by-a-space?noredirect=1#comment75830321_44414553
 */
error_reporting(E_ALL);

// UNABLE TO ACCESS: http://events.uconn.edu/exports-2017-05-31-2017-07-01.csv
// COPY MADE HERE FROM THE DOWNLOAD LINK
$url = 'storage/exports-2017-05-31-2017-07-01.csv';
$csv = file_get_contents($url);
echo $csv;

$fpr = fopen($url, 'r');
if (!$fpr) trigger_error("Unable to open $url", E_USER_ERROR);

$kount = 0;
while (!feof($fpr))
{
    $row = fgetcsv($fpr);
    $kount++;
    if (!$row) break;
    echo PHP_EOL . count($row);
}

echo PHP_EOL . "There are $kount rows in the CSV at $url";