The Old County The Old County - 5 months ago 19
PHP Question

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

I am trying to do a page scrape and put the collection of items into a CSV file:

[title] => Ayrshire Brewers Ltd
[url] =>
[metadata] => (Stevenston, Ayrshire and Arran 1981-1982)
[start] => 1981
[end] => 1982

^ so an item would look like this.
--- things go awry when I put the results into the csv -- the comma delimiter mucks up with the metadata comma for example.
-- if I eradicate ALL commas in the data, then it imports smoothly - but this surely can not be the way - how can I escape the comma's in content. I've tried putting quotes around the variable during csv import but it comes back with an error.

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



The answer that worked - after resolving other bugs in the code, cleaning up html, adding gap columns in the right place if needed.

function makeCSV2($filename, $list){
        $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);