Timothée HENRY
Timothée HENRY

Reputation: 14604

PHP fgetcsv - detecting too many records

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).

Upvotes: 0

Views: 233

Answers (2)

Rully
Rully

Reputation: 154

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;

Upvotes: 1

senz
senz

Reputation: 2158

This string is not correctly escaped:

"
\""&gt;.&lt;/&gt;"";
"

All quote chars must have backslashes before them (or other escape char that you've passed into appropriate param. And you should't pass 0 and comma to fgetcsv, they are already default: http://php.net/fgetcsv

Upvotes: 1

Related Questions