我有一个CSV文件,其内容如下。
1 2 3 4 \ t 45 56 67
如上所示,在值4之后,我有一个标签空间。我只需要读取值,直到制表空间。我可以打开CSV文件并读取直到制表符分隔符,如下所示。
$file = fopen("outputfile.csv","r");
//I am reading till tab space.
while ($line = fgetcsv($file, 0, "\t") !== false)
但是,现在我需要读取所有值,直到制表符空间进入PHP数组进行一些操作。我怎样才能实现同样的目标?
答案 0 :(得分:2)
尝试:
while ($line = fgetcsv($file, 0, "\t") !== false) {
$columns = str_getcsv($line, ' '); //or use explode()
}
答案 1 :(得分:1)
explode()是要走的路。
$file = fopen("outputfile.csv","r");
while ($line = fgetcsv($file, 0, "\t") !== false)
{
$array = explode(' ', $line);
}
答案 2 :(得分:1)
我会将该行拆分为“before tab”和“after tab”组件,并且只对“before tab”一半进行解析:
$file = fopen("outputfile.csv","r");
// for each line in the file, until EOF
while( ($line = fgets($file)) !== false) {
// split out the tab char:
$beforeTab = explode( "\t", $line)[0];
// now, parse the CSV part
$parsedCSV = str_getcsv( $beforeTab);
// do what you need with the parsedCSV array.
}