我正在尝试将CSV字符串解析为PHP中的数组.CSV字符串具有以下属性:
Delimiter: ,
Enclosure: "
New line: \r\n
Run Code Online (Sandbox Code Playgroud)
示例内容:
"12345","Computers","Acer","4","Varta","5.93","1","0.04","27-05-2013"
"12346","Computers","Acer","5","Decra","5.94","1","0.04","27-05-2013"
Run Code Online (Sandbox Code Playgroud)
当我尝试解析它时:
$url = "http://www.url-to-feed.com";
$csv = file_get_contents($url);
$data = str_getcsv($csv);
var_dump($data);
Run Code Online (Sandbox Code Playgroud)
最后一个元素和一个元素串在一个字符串中:
[0]=> string(5) "12345"
...
[7]=> string(4) "0.04"
[8]=> string(19) "27-05-2013
"12346""
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?任何帮助,将不胜感激.
我有一个csv文件,看起来像这样
$lines[0] = "text, with commas", "another text", 123, "text",5;
$lines[1] = "some without commas", "another text", 123, "text";
$lines[2] = "some text with commas or no",, 123, "text";
Run Code Online (Sandbox Code Playgroud)
我想要一张桌子:
$t[0] = array("text, with commas", "another text", "123", "text","5");
$t[1] = array("some without commas", "another text", "123", "text");
$t[2] = array("some text, with comma,s or no", NULL , "123", "text");
Run Code Online (Sandbox Code Playgroud)
如果我使用split($lines[0],",")我会得到"text" ,"with commas" ...
有没有任何优雅的方式来做到这一点?
考虑像这样的CSV文件
item1,"description 1"
item2,"description 2"
item3,"description 3
description 3 continues on new line"
item4,"description 4"
Run Code Online (Sandbox Code Playgroud)
哪个应该像这样解析
item1,"description 1"
item2,"description 2"
item3,"description 3 description 3 continues on new line"
item4,"description 4"
Run Code Online (Sandbox Code Playgroud)
有没有办法在PHP中解析具有多行值的CSV?
我有一个csv文件,我想用PHP.csv中的第一件事是列名.一切都用逗号分隔.
我希望能够将列名称作为数组名称,并且该列的所有值都将在该数组名称下.因此,如果column1下有20行,那么我可以执行column1 [0],并且第一个实例(不是列名)将显示为column1.
我该怎么办?
我正在尝试在 PHP 中创建一个多维数组,其中内部数组与以下示例 CSV 字符串 $csv 相关联:
# Results from 2015-06-16 to 2015-06-16.
date,time,label,artist,composer,album,title,duration
2015-06-16,12:00 AM,Island,U2,"Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr",Songs Of Innocence,SONG FOR SOMEONE,03:46
2015-06-16,12:04 AM,Lowden Proud,"Fearing & White, Andy White, Stephen Fearing","White- Andy,Fearing- Stephen",Tea And Confidences,SECRET OF A LONG LASTING LOVE,03:10
2015-06-16,12:07 AM,Columbia,The Wallflowers,"Dylan- Jakob,Irons- Jack,Mathis- Stuart,Richling- Greg,Jaffee- Rami",Glad All Over,REBOOT THE MISSION,03:31
2015-06-16,12:10 AM,Distort Light,Bend Sinister,Moxon- Daniel,"Stories Of Brothers, Tales Of Lovers",JIMMY BROWN,03:48
Run Code Online (Sandbox Code Playgroud)
第 3+ 行格式后的实际数据行数是可变的。到目前为止我所做的是一个普通的多维数组:
$resultArray = str_getcsv($csv, PHP_EOL);//parse the rows
array_shift($resultArray);//shift out results first row: date …Run Code Online (Sandbox Code Playgroud)