CSV导入错误,内容中有逗号,被误认为是下一列

CSV import error, with commas in the content, being mistaken for the next column

我正在尝试抓取页面并将项目集合放入 CSV 文件中:

Array
        (
            [title] => Ayrshire Brewers Ltd
            [url] => http://www.quaffale.org.uk/php/brewery/982
            [metadata] => (Stevenston, Ayrshire and Arran    1981-1982) 
            [start] => 1981
            [end] => 1982
        )

^ 所以一个项目看起来像这样。 --- 当我将结果放入 csv 时出现问题 - 例如,逗号分隔符与元数据逗号混在一起。 -- 如果我消除了数据中的所有逗号,那么它会顺利导入 - 但这肯定不是这样 - 我如何才能转义内容中的逗号。我试过在 csv 导入期间在变量周围加上引号,但返回时出现错误。

function makeCSV($filename, $ret){

        $list = array();

        //if array is not empty
        if(key_exists(0, $ret)){

            $keylist = array_keys($ret[0]);
            foreach ($keylist as $key => $value){
                $list[] = $value;
            }
            $implode = implode(",",$list);

            $list = array($implode);

            //put into csv
            foreach($ret as $x => $x_value) {
                $list[] = implode(",",$x_value);
            }
        }



        $file = fopen($filename.".csv","w");
        foreach ($list as $line){
            fputcsv($file,explode(',',$line));
        }

        fclose($file);       
    }

有效的答案 - 在解决代码中的其他错误后,清理 html,根据需要在正确的位置添加间隙列。

function makeCSV2($filename, $list){
        //header
        $heads = array("title", "brewurl", "metadata", "start", "end", "Address", "lat", "lng", "County (see footnote)", "Phone", "Mobile", "Web", "weburl", "facebook", "twitter", "e-mail", "emailaddress", "History", "Beers Brewed", "Regular Outlets", "Visit Information", "Brewery Shop Information", "postcode");

        array_unshift($list, $heads);//adds the header to the top of the array

        $fp = fopen($filename.".csv","w");

        foreach ($list as $fields){
            fputcsv($fp, $fields);
        }
        fclose($fp); 
    }