0

I have a csv file that I read and save as an output file using the following code :

CSV content :

10038,"Service, Inc.",loten,[email protected],9951,xxx,6321
10041,Roadsi deInc.,Kgel,[email protected],1101,xxx,7967
10042,Rang LLC,Resgers,[email protected],2073,4611,xxx


<?php
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    if (($handle = fopen(__DIR__.'/test.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]
            );
            fputcsv($fp, $data_row, "|");
        }
    }
    fclose($handle);
    fclose($fp);

the results I get is like the folowing :

10038|"Service, Inc."|loten|[email protected]|9951|xxx
10041|"Roadsi deInc."|Kgel|[email protected]|1101|xxx
10042|"Rang LLC"|Resgers|[email protected]|2073|4611

how can I remove the quotes that the fputcsv function added when there is a space in a string and how can I add a 0d0a ending to each line of the csv output?

5
  • RTFM? php.net/fgetcsv There's options you can use to list separators and enclosure chars. Commented Jul 11, 2014 at 21:55
  • If I read it , if I had a solution I would not ask here Commented Jul 11, 2014 at 22:20
  • You should read php.net/manual/en/function.fputcsv.php actually since you want to change the default enclosure. Your answer is there, right at the top. Commented Jul 11, 2014 at 23:48
  • I did add the folowing : while (($data = fgetcsv($handle, 1000, ",",'"')) !== FALSE) { but the quotes are still there in the output file Commented Jul 11, 2014 at 23:54
  • what about the line ending with 0d0a? Commented Jul 11, 2014 at 23:54

1 Answer 1

2

The quotes are added by fputcsv, try fputs and join:

<?php
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    if (($handle = fopen(__DIR__.'/test.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]
            );
            fputs($fp, join("|", $data_row)."\r\n";
        }
    }
    fclose($handle);
    fclose($fp);
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I get a worng result :only one line 10038|"Service, Inc."|loten|[email protected]|9951|xxx|" , quote added at the end also I had to put : fputcsv($fp, $data_row, "|", '"'); otherwise fput shows an error
Updated, using fputs and join instead of fputcsv.

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.