简体   繁体   中英

PHP issue in csv output with quotes

Hello I have a csv file when I open it in excel it looks like clean with no quotes at the starting or the ending of each line, same thing in wordpad it looks like this :

10038,"Service, Inc.",loten,4u@att.net,9951,xxx,6321
10041,RoadsideInc.,Kgel,gel1980@gmail.com,1101,xxx,7967
10042,RangLLC,Resgers,resranger85@yahoo.com,2073,4611,xxx
10050,5-Dg,5-dg,pik37@gmail.com,3-4874,2838389,xxx
10053,SandserviceInc.,Service Center Inc.,br@sands.com,6-5556,861398,xx
10055,CenStammddy ,Mman,jooan@an.com,6-1449,xx,36-3755
10060,"BetsyRES, C",ElHies,hines@ol.com, 8-0306,237602,xx
10061,Hampnemes LLC,DBERG@gmail.com,83-7999,xxx,-74-1043,xx
10066,CarReal LLC,CarPRea LLC,selby@c.r.com,8889,456440,xx

when I read that file in PHP via the script below I get the valid result I want wich is like the folowing :

Array
(
    [0] => 10038|Service, Inc.|loten|4u@att.net|9951|xxx
    [1] => 10041|RoadsideInc.|Kgel|gel1980@gmail.com|1101|xxx
    [2] => 10042|RangLLC|Resgers|resranger85@yahoo.com|2073|4611
    [3] => 10050|5-Dg|5-dg|pik37@gmail.com|3-4874|2838389
    [4] => 10053|SandserviceInc.|Service Center Inc.|br@sands.com|6-5556|861398
    [5] => 10055|CenStammddy |Mman|jooan@an.com|6-1449|xx
    [6] => 10060|BetsyRES, C|ElHies|hines@ol.com| 8-0306|237602
    [7] => 10061|Hampnemes LLC|DBERG@gmail.com|83-7999|xxx|-74-1043
    [8] => 10066|CarReal LLC|CarPRea LLC|selby@c.r.com|8889|456440
)

when I read the file and save the array I get into a new csv file as output using the PHP code down below I get an output csv file that looks ok in excel but opened in wordpad I see many lines that start and end with quotes like the following :

"10038|Service, Inc.|loten|4u@att.net|9951|xxx"
10041|RoadsideInc.|Kgel|gel1980@gmail.com|1101|xxx
10042|RangLLC|Resgers|resranger85@yahoo.com|2073|4611
10050|5-Dg|5-dg|pik37@gmail.com|3-4874|2838389
"10053|SandserviceInc.|Service Center Inc.|br@sands.com|6-5556|861398"
"10055|CenStammddy |Mman|jooan@an.com|6-1449|xx"
"10060|BetsyRES, C|ElHies|hines@ol.com| 8-0306|237602"
"10061|Hampnemes LLC|DBERG@gmail.com|83-7999|xxx|-74-1043"
"10066|CarReal LLC|CarPRea LLC|selby@c.r.com|8889|456440"

Is there something wrong with the way I'm saving the csv output file please?

<?php

function createCSV($rows){
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    foreach ($rows as $field) {
      fputcsv($fp, array($field));
    }
    fclose($fp); 
    echo "csv created";
}
$result = array();
        $row = 1;
        if (($handle = fopen(__DIR__.'/stack.csv', "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                    $data[0],
                    $data[1],
                    $data[2],
                    $data[3],
                    $data[4],
                    $data[5]
                 );                            
                $result[] = implode("|", $data_row);
                $row++;
            }
        }
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        fclose($handle);
        createCSV($result);

the csv input and output files are below :

input csv

output csv

Hi try saving the data like this

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' ]] )

OR in your case

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                    $data[0],
                    $data[1],
                    $data[2],
                    $data[3],
                    $data[4],
                    $data[5]
                 );                            
               // $result[] = implode("|", $data_row); -- change this line to
                 $result[] =  $data
                $row++;
            }

change this function to include your delemiter, and don't wrap $field ( which is an array ) in another array

function createCSV($rows){
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    foreach ($rows as $field) {
      fputcsv($fp, $field, "|");
    }
    fclose($fp); 
    echo "csv created";
}

Update here is a more efficient way to do it ( you don't need to build the results array and loop 2 times )

        $csv_filename = "output.csv";
        $fp = fopen($csv_filename, "w");
        if (($handle = fopen(__DIR__.'/stack.csv', "r")) !== FALSE && $fp) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                        $data[0],
                        $data[1],
                        $data[2],
                        $data[3],
                        $data[4],
                        $data[5]
                );

                echo "<pre>";
                print_r($data_row);
                echo "</pre>";
                fputcsv($fp, $data_row, "|");
            }
        }

        fclose($handle);
        fclose($fp);
        echo "csv created"

You need to change the function call you use to write on disk as follows:

fputcsv($fp, array($field),"|","");

This because the last parameter is the one defining the enclosure string of your lines, as described in the php manual :

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' ]] )

And the default value is the quote you want to remove

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM