我正在导入的文件中的字符串:1,2,3,"a","b","c","1,2,3,4,5",6,7
所需的输出是由逗号分割的数组,但忽略任何位于双引号之间的逗号。
[0] = 1;
[1] = 2;
[2] = 3;
[4] = "a";
[5] = "b";
[6] = "c";
[7] = "1,2,3,4,5";
[8] = 6;
[9] = 7;
需要使用PHP 5.2及更低版本。我知道fget_csv函数允许在PHP 5.3中使用它,但不幸的是它不是一个选项。
搜索相当广泛,到目前为止我找不到解决方案。有什么想法吗?
答案 0 :(得分:3)
您可以在PHP中执行csv< 5.3:
$lines = array();
if (($handle = fopen('file.csv', 'r')) !== FALSE) {
while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) {
$lines[] = $line;
}
fclose($handle);
}
print_r($lines);
/* output:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => a
[4] => b
[5] => c
[6] => 1,2,3,4,5
[7] => 6
[8] => 7
)
)
*/