PHP, CSV file for array, html values

I use this code to read any CSV file to build an array, so I can use it later, the problem is that this particular CSV file has columns with html

$csv = array_map('str_getcsv', file('docs/foo.csv'));
$headers = $csv[0];
unset($csv[0]);
$csvarray = [];
foreach ($csv as $row) {
    $newRow = [];
    foreach ($headers as $k => $key) {
        $newRow[$key] = $row[$k];
    }
    $csvarray[] = $newRow;
}

it works fine with any other CSV file, but for that particular CSV it just doesn't work:

Csv example:

cat,name,des,ldes,datas
1,foo1,desfoo1,<h1>foo</h1><p class="finetext">sometext</p>, data1,data2,data3
3,foo3,desfoo3,<h1>foo3</h1><p class="finetext">sometext</p>, data4,data6

the array i got looks something like this:

array (
[cat]=>1
[name]=>foo1
[des]=>desfoo1
[ldes]=>foo
... after this everything breaks...

So how can I build an array from this CSV file that has coulmns / values ​​with html ...

UPDATE

[1] => Array
    (
        [cat] => 12
        [name] => Foo no html
        [des] => From foo no html
        [ldes] => 'Basic long desc with htmls
    )
[2] => Array
    (
    [0] =>
    )
[3] => Array
    (
    [0] =>
    basic next row html
    some text inside p with class
    )

This is the array I get, it just breaks into one array of senses ...

+4
source share
2 answers

So you can accept my comment as an answer, here is the same comment.

. - Windows, Unix- ( ). .

, (, HTML).

0

, , , , - " print_r $csvarray, . print_r, kint

http://raveren.imtqy.com/kint/

$csv = array_map('str_getcsv', file('docs/foo.csv'));
$headers = $csv[0];
unset($csv[0]);
$csvarray = [];
foreach ($csv as $row) {
    $newRow = [];
    foreach ($headers as $k => $key) {
        $newRow[$key] = htmlentities ($row[$k]);
    }
    $csvarray[] = $newRow;
}

print_r($csvarray);
+1

All Articles