我将CSV文件导入MySQL数据库。要使用fgetcsv()
解析CSV,请使用error array_combine(): Both parameters should have an equal number of elements
。 CSV包含"未被转义并导致"GR109 "," ",0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0," ","GRANT ","M ","W",0,0.0,0.0,0.0,0.0,0.0,0,0,0,0,0,0.0,0.0," ",0,0,0.0," "," "," ",2.42,0.0,0.0,0.0,0.0," "," "," "," "," "," ",0.0,0.0,0.0,0.0,0.0," "," "," "," ","SELF COL ","16 P PR. "," ","PLAIN "," ","R/E1ROW "," "," "," "," "," "," "," ","R/E1ROW ","BEADED "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","GRANT ","GRANT "," "," "," "," ",0.0," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","VAMP LNG - BLK. CARAVELLE P/S. QTR. LNG./ TNG.LINING - BLK. TORINO. (GREY ""TORINO"" FOR LIZARD.)","GR109 COMPLETE"
CSV数据采用以下格式:
function csv_to_array($filename='', $delimiter=',', $enclosure='"', $escapestring='"')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure, $escapestring)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
我的代码:
$escapestring='"'
}
我添加了command line:
Pros:
easy to use, reliable (I got a quality loss problem with library mode), many tutorials
Cons:
the is is a separate process, out of our control, we can't get detailed information of the process, like the video conversion progressing data
the command line way seems not so cool, this is not well integrated in the whole project.
libav* library:
Pros:
we can have the process detailed data, progressing status....
...
Cons:
difficult to use, much less materials
,但这没有任何帮助。错误来自"" TORINO""在哪里"角色没有逃脱?如果是这样,有没有办法处理这些字段?
答案 0 :(得分:1)
您的代码对我来说非常好。在下面的示例中,我删除了标头处理,因为您的示例数据不包含任何标头。
<?php
function csv_to_array($filename='', $delimiter=',', $enclosure='"', $escapestring='"')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure, $escapestring)) !== FALSE) {
$data[] = $row;
}
fclose($handle);
}
return $data;
}
$result = csv_to_array('test.csv');
foreach ($result as $key=>$element) {
echo $key . ' => ' . print_r($element,true) . "\n";
}
如果文件test.csv
包含您给定的csv字符串,那么明显的输出是(缩短的):
0 => Array
(
[0] => GR109
[1] =>
[2] => 0.0
[3] => 0.0
// .....
[127] =>
[128] => VAMP LNG - BLK. CARAVELLE P/S. QTR. LNG./ TNG.LINING - BLK. TORINO. (GREY "TORINO" FOR LIZARD.)
[129] => GR109 COMPLETE
)
答案 1 :(得分:0)
$result = preg_replace('/"((?=[^"]*)(?=(?=[^"]*)))"/', '$1', $subject);
上面的正则表达式将删除双引号内的双引号。 它适用于:
引号“更多文字”中的“一些文字”
“”内部引用“更多文字”
答案 2 :(得分:0)
确定。我想我找到了。绝对是fgetcsv
中的第二个参数,这是该行的长度,搞砸了一点。只需将其更改为0(函数可能工作得相当慢),或加倍。
当它为1000时,它将每行切成两排,一排长度恰好为1000个字符(即使在世界中间也进行了切割),第二行是换行符的换行符。所以$ row变量最初是一个长度为117的数组,然后是13,再次是117和13.
只需改变一下:
while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure, $escapestring)) !== FALSE)
到此:
while (($row = fgetcsv($handle, 0, $delimiter, $enclosure, $escapestring)) !== FALSE)