如何使用PHP解析每行用方括号括起来的CSV?

时间:2014-12-15 15:30:12

标签: php csv

在尝试传递从伦敦交通局收到的Feed时,我偶然发现了CSV-ish格式。使用PHP解析它的最佳方法是什么?

http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?circle=51.49288,-0.147425,100&ReturnList=StopPointName,EstimatedTime,StopID

[4,"1.0",1418639278611]
[1,"Eccleston Bridge","35312",1418639472000]
[1,"Eccleston Bridge","35312",1418639395000]
[1,"Eccleston Bridge","35312",1418639397000]
[1,"Eccleston Bridge","35312",1418639696000]
[1,"Eccleston Bridge","35312",1418639742000]
[1,"Eccleston Bridge","35312",1418639731000]
[1,"Eccleston Bridge","35312",1418640051000]
[1,"Eccleston Bridge","35312",1418639938000]
...

感谢。

4 个答案:

答案 0 :(得分:2)

由于每行单独看起来像JSON,您可以在此使用json_decode()

// input as string
$input = '....';

// split by lines
$lines = explode( "\n", $input );

// walk through results
for( $i=0; $i<count($lines); $i++ ) {

  // parse row
  $row = json_decode( $lines[i] );

  // do something with it ...

}

答案 1 :(得分:1)

将每行解析为单独的字符串后,您可以使用substr()修剪括号。

例如:

echo substr("1418639472000]",0,-1);
> 1418639472000

echo substr("[1234",1);
> 1234

答案 2 :(得分:0)

这将为每行提供一个关联数组

$file = file('http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?circle=51.49288,-0.147425,100&ReturnList=StopPointName,EstimatedTime,StopID');

array_walk($file, function(&$line) {
    $line = json_decode($line);
    if (count($line) == 3) {
        $line = array_combine(array('id', 'count', 'timestamp'), $line);
    } else {
        $line = array_combine(array('id', 'station_name', 'station_id', 'timestamp'), $line);
    }
});

print_r($file);

答案 3 :(得分:0)

您不需要重复调​​用 json_decode()。因为严格格式化的字符串几乎是有效的 json,所以您只需要用逗号替换换行符,然后将该单个字符串包裹在方括号中,然后 json_decode() 准备吞噬字符串并吐出索引的索引数组数组。请注意,在某些系统上,您可能需要替换 \r\n 而不仅仅是 \n

代码:(Demo)

$bracedCSV = <<<CSV
[4,"1.0",1418639278611]
[1,"Eccleston Bridge","35312",1418639472000]
[1,"Eccleston Bridge","35312",1418639395000]
[1,"Eccleston Bridge","35312",1418639397000]
[1,"Eccleston Bridge","35312",1418639696000]
[1,"Eccleston Bridge","35312",1418639742000]
[1,"Eccleston Bridge","35312",1418639731000]
[1,"Eccleston Bridge","35312",1418640051000]
[1,"Eccleston Bridge","35312",1418639938000]
CSV;

var_export(
    json_decode('[' . str_replace("\n", ',', $bracedCSV) . ']')
);

输出:

array (
  0 => 
  array (
    0 => 4,
    1 => '1.0',
    2 => 1418639278611,
  ),
  1 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639472000,
  ),
  2 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639395000,
  ),
  3 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639397000,
  ),
  4 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639696000,
  ),
  5 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639742000,
  ),
  6 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639731000,
  ),
  7 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418640051000,
  ),
  8 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639938000,
  ),
)