PHP fgetcsv - 检测到太多记录 [英] PHP fgetcsv - detecting too many records
问题描述
我有以下csv文件:
Id,Title,Body,Tags
101,this title,
\& gt;。& lt; /& gt;;
,c#asp.net excel表
,我想将其转换为数组如下:
数组
pre>
(
[0] => Array
(
[0] =& b $ b [1] =>标题
[2] => Body
[3] =>标签
)
[1] => ; Array
(
[0] => 101
[1] => this title
[2] => \& gt; ; lt; /";
[3] => c#asp.net excel表
)
)
我的代码是:
while(($ data = fgetcsv($ handle,0,,))!== FALSE){
$ num = count($ data);
for($ c = 0; $ c <$ num; $ c ++){
$ data [$ c] = strip_tags($ data [$ c]);
}
$ result [$ row] = $ data;
$ row ++;
}
fclose($ handle);
return $ result;
我的问题是我得到以下数组:
数组
(
[0] => Array
(
[0] => Id
[1] =>标题
[2] =>正文
[3] =>标签
)
[1] =& b $ b(
[0] => 101
[1] =>此标题
[2] =>
\> >;
)
[2] => Array
(
[0] =>,c#asp.net excel table
)
)
一般来说,许多recor当在字段中有潜在的代码(它是一个StackOverflow数据转储,所以一些文本字段有各种编程代码)。
解决方案尝试使用 CSVed 打开文件,以确保其格式正确,为CSV格式。
如果CSV被破坏,那么你可以做一些快速修复解析的结果。例如:
while(($ data = fgetcsv($ handle,0,,))!== FALSE) {
$ num = count($ data);
for($ c = 0; $ c <$ num; $ c ++){
$ data [$ c] = strip_tags($ data [$ c]);
}
if(count($ data)== 3){
$ data [1] [2]。= $ data [2]。
unset($ data [2]);
}
$ result [$ row] = $ data;
$ row ++;
}
fclose($ handle);
return $ result;
I have the following csv file:
"Id","Title","Body","Tags" "101","this title"," \"">.</>""; ","c# asp.net excel table"
which I want to convert into an array as follows:
Array ( [0] => Array ( [0] => Id [1] => Title [2] => Body [3] => Tags ) [1] => Array ( [0] => 101 [1] => this title [2] => \"">.</>""; [3] => c# asp.net excel table ) )
My code is:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $data[$c] = strip_tags($data[$c]); } $result[$row] = $data; $row++; } fclose($handle); return $result;
My problem is I am getting the following array:
Array ( [0] => Array ( [0] => Id [1] => Title [2] => Body [3] => Tags ) [1] => Array ( [0] => 101 [1] => this title [2] => \">.</>""; ) [2] => Array ( [0] => ,c# asp.net excel table" ) )
In general, how do I avoid detecting too many recors when there is potentially code inside the fields (it's a StackOverflow data dump so some text fields have all kinds of programming code).
解决方案Try opening the file using CSVed to make sure that it was properly formatted as CSV.
If the CSV is broken, then you can do some quick fix to the parsed result. For example:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $data[$c] = strip_tags($data[$c]); } if (count($data) == 3) { $data[1][2] .= $data[2].[0]; unset($data[2]); } $result[$row] = $data; $row++; } fclose($handle); return $result;
这篇关于PHP fgetcsv - 检测到太多记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!