Avoid double quotation marks in CSV file exported with PHP

0

It is possible to prevent the CSV file from coming with aspas duplas in string fields, I am using the script below, but the nome field is coming between aspas duplas , how can I avoid this?

<?php//export.phpif(isset($_POST["export"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "simrede"); 
      if (mysqli_connect_errno())
      {
      echo "Falha ao fazer conexão: " . mysqli_connect_error();
      }

    // Set utf8
      mysqli_set_charset($connect,"utf8");
      $connect->set_charset('utf8');
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=Gabaritos_OMR_Alunos-Simrede.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('ROLLNO', 'NAME', 'CLASS', 'EMAILID', 'PHONENO'),';');  
      $query = 'SELECT ROLLNO, nome, concat(".",nivel,"ano"), concat(id,"@gmail.com"), siem_id from cs_gabarito';  
      $result = mysqli_query($connect, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {
           fputcsv($output, $row,";");  
      }  
      fclose($output);  
 }  
 ?>
  

Edit
  The answer from Júlio Neto using fwrite and implode served, it was enough to add line break in the last field of the query to avoid joining the last field with the   first of each line, as follows:

..., concat(siem_id,"\n") from cs_gabarito';  
    
asked by anonymous 01.10.2018 / 19:37

2 answers

1

There are two solutions, one is to pass an empty string in the fourth argument of the fputcsv function:

fputcsv($output, $row, ";", "");  

Or rewrite the line that writes to the file the data, using the implode to join the records:

fwrite($output, implode(";", $row));

Try one of the two and say if it works.

    
01.10.2018 / 19:57
0

The fputcsv function always places an enclosure character > (which by default is " --- double quotation mark) the field being treated has spaces. If the field already contains a value equal to enclosure it doubles. In this case: sou um "campo" com aspas would "sou um ""campo"" com aspas" .

Theoretically, this does not matter much if you use fgetcsv because the function treats this automatically.

If you implement something at hand, you must always remember to escape the characters of the string that conflict with the characters you are using as a separator.

Here is an example showing the behavior of the function.

    
01.10.2018 / 20:06