1

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

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
        )

^ 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){
            fputcsv($file,explode(',',$line));
        }

        fclose($file);       
    }
8
  • Thank you for the link - Are you able to demonstrate the solution with an accurate correction of the code Aug 19, 2016 at 18:04
  • If you have a solution to refactoring a multi-dimensional keyed array - that doesn't appear in the documentation - that will resolve this comma issue - be much appreciated. Aug 19, 2016 at 18:11
  • If I could double down vote this, I would. Why would you ask someone to "correct" your code when you're not willing to read the documentation and do it yourself? Aug 19, 2016 at 18:11
  • 1
    the dimensions of your array are utterly irrelevant. fputcsv only deals with a single dimension/row/record at a time anyways. And what's in your array is irrelevant as well. if you'd bothered actually READING the link manual page, you'd see the option you have to give fputcsv to fix your problem. you don't change your array/data. you change how fputcsv DEALS with the data
    – Marc B
    Aug 19, 2016 at 18:14
  • 1
    The problem in the code is $implode = implode(",",$list); - you are building the CSV line yourself, which is circumventing the purpose of the CSV function. You can move the fputcsv() here to encode the line correctly (PHP will then do the work of wrapping elements with quotes, so that commas are interpreted correctly).
    – halfer
    Aug 19, 2016 at 18:47

1 Answer 1

1

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){
        //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); 
    }
2
  • 1
    @halfer - the solution found Aug 22, 2016 at 11:49
  • 1
    __So it imports correctly now -- it didn't help that there were gaps during the scrape - so like some pages didn't have "mobile" - so I had to check for things like this and add empty "coloumns" so to speak. Aug 22, 2016 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.