0

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,[email protected],9951,xxx,6321
10041,RoadsideInc.,Kgel,[email protected],1101,xxx,7967
10042,RangLLC,Resgers,[email protected],2073,4611,xxx
10050,5-Dg,5-dg,[email protected],3-4874,2838389,xxx
10053,SandserviceInc.,Service Center Inc.,[email protected],6-5556,861398,xx
10055,CenStammddy ,Mman,[email protected],6-1449,xx,36-3755
10060,"BetsyRES, C",ElHies,[email protected], 8-0306,237602,xx
10061,Hampnemes LLC,[email protected],83-7999,xxx,-74-1043,xx
10066,CarReal LLC,CarPRea LLC,[email protected],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|[email protected]|9951|xxx
    [1] => 10041|RoadsideInc.|Kgel|[email protected]|1101|xxx
    [2] => 10042|RangLLC|Resgers|[email protected]|2073|4611
    [3] => 10050|5-Dg|5-dg|[email protected]|3-4874|2838389
    [4] => 10053|SandserviceInc.|Service Center Inc.|[email protected]|6-5556|861398
    [5] => 10055|CenStammddy |Mman|[email protected]|6-1449|xx
    [6] => 10060|BetsyRES, C|ElHies|[email protected]| 8-0306|237602
    [7] => 10061|Hampnemes LLC|[email protected]|83-7999|xxx|-74-1043
    [8] => 10066|CarReal LLC|CarPRea LLC|[email protected]|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|[email protected]|9951|xxx"
10041|RoadsideInc.|Kgel|[email protected]|1101|xxx
10042|RangLLC|Resgers|[email protected]|2073|4611
10050|5-Dg|5-dg|[email protected]|3-4874|2838389
"10053|SandserviceInc.|Service Center Inc.|[email protected]|6-5556|861398"
"10055|CenStammddy |Mman|[email protected]|6-1449|xx"
"10060|BetsyRES, C|ElHies|[email protected]| 8-0306|237602"
"10061|Hampnemes LLC|[email protected]|83-7999|xxx|-74-1043"
"10066|CarReal LLC|CarPRea LLC|[email protected]|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

2
  • 2
    You are using implode("|", $data_row) to fix all items into one so that's what you get: one value. Commented Jul 11, 2014 at 17:06
  • When theres a space, the csv will surround the string with quotes to show its one thing Commented Jul 11, 2014 at 17:25

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I did this : $data_row = $data[0]."|".$data[1]."|".$data[2]."|".$data[3]."|".$data[4]."|".$data[5]; array_push($result, str_replace('"', '', $data_row)); and I still have quottes at the bigening and the ending of some line when I open the file in wordpad
because that is the same as implode, just leave it in an array, and write it with fputcsv with the "|" as the delimiter, as in my example.
your exemple is giving Array in each csv line that is why I had to do like I showed, can you correct it please?
@user3593154 - that was the original problem then adding the implode just hid it. did you change fputcsv($fp, array($field)); to fputcsv($fp, $field); without the array? -- updated the answer for that.
0

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

3 Comments

there is no quote, OP is sending array( 10066|CarReal LLC|CarPRea LLC|[email protected]|8889|456440 ) which because it has spaces and is a single field, ie 1 string with spaces it correctly gets quoted by fputcsv
Check the output... You have quotes at the end and beginning of lines that you don't have in the original cvs... OP wrote: "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...". So the problem OP is talking about IS regarding quotes
what do you mean something like that : fputcsv($fp, array($field),"|",'"'); ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.