在csv字符串中找不到换行符


Can't find newline in csv string

使用file_get_contents()从外部URL检索CSV数据。在此数据中,CSV中的每个新记录都有可见的中断。但是,当我尝试扩展行时,我只得到数组中的一个项,它包含完整的CSV数据集。下面是我使用的代码:

$data = file_get_contents('http://example.url/csvdata.asp?id=20'); // has 12 records
$rows = explode("'n", $data);
echo count($rows); // returns 1

这是否与ASP脚本生成CSV有关?他们是否使用其他新行字符?

使用文件让PHP处理新行

file -将整个文件读入数组

你可以简单地做

$rows = file('http://example.url/csvdata.asp?id=20'); 

'n可以工作,但有更多的新行方式。这应该可以工作:

$lines = preg_split("/''r''n|''r|''n/", $data);

我也有同样的问题。我在SO:

上找到了这个。
$rows = explode(PHP_EOL, $data);

PHP_EOL找到行尾