PHP,CSV文件到数组,html值

我正在使用此代码读取任何CSV文件来构建一个数组,以便我以后可以使用它,我遇到的问题是这个特定的CSV文件有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;
}

它适用于任何其他CSV文件,但对于这个特定的CSV它只是不起作用:

示例csv:

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

我得到的数组是这样的:

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

那么如何在这个CSV文件中构建一个数组,该文件具有带html的coulmns /值…

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
    )

这是我得到的数组,它只是分解为一个无意义的数组……

最佳答案 所以你可以接受我的评论作为答案,这是相同的评论.

I would see two possible problems. Line change is either Windows or Unix-like (one or the other is not managed correctly). Or the encoding is scrapping it all.

我会在一行中添加换行符(如HTML部分).

点赞