1

I've got a HTML form, which I need to store in DB, then write it to .csv and .txt . Everything work well, except the txt output give me some bad string.

fputcsv function:

private function fputcsv_custom1($fp, $array, $eol)
{
  fputcsv($fp, $array, ';');
  if ("\r\n" != $eol && fseek($fp, -1, SEEK_CUR) === 0) 
  { fwrite($fp, $eol . "\r\n"); }
}

Controller:

$peoples = OnlineSz::all()->toArray();

$file = fopen("online_hotel.csv", "w+");
$file1 = fopen("online_hotel.txt", "w+");

array_keys($peoples);

foreach ($peoples as $key => $array) {
  $this->fputcsv_custom($file, $array, ';');
  $this->fputcsv_custom1($file1, $array, ';');
}
fclose($file);
fclose($file1);

It generates me 2 file, which separated by ; and the last value give a \r\n to new line.

The txt output is the next:

53;"New test";Worker;test;test;4000;test;test;06123456789;test;0;0;CC;;0;0;

If the string contain whitespace, it's stored by " " , how can i remove this quotes? Or how can i replace the quotes only this column from an array?

Thanks for the help!

EDIT

If i replace the space between 2 word, the quotes are not there, but what can i do if i need the space between the string?

Controller:

$this->fputcsv_custom($file4, str_ireplace(chr(32), '/',$user), ';');
$this->fputcsv_custom1($file5, str_ireplace(chr(32), '/',$user), ';');

Output:

53;New/test;Alkalmazott;test;test;4000;test;test;06123456789;0;0;CC;0;0;

But this i wrong, because the /, and if i replace it to chr(32), the quotes will be there.

Any solution?

fletcher
  • 58
  • 8

2 Answers2

0

Use str_ireplace to remove the quotes....

$var  = str_ireplace('"', '', $var); //strip quotes from $var



 $this->fputcsv_custom($file, str_ireplace('"', '',$array), ';');
 $this->fputcsv_custom1($file1, str_ireplace('"', '',$array), ';');

You might also want to trim them of trailing spaces....

 $this->fputcsv_custom($file, trim(str_ireplace('"', '',$array)), ';');
 $this->fputcsv_custom1($file1, trim(str_ireplace('"', '',$array)), ';');
Duane Lortie
  • 1,285
  • 1
  • 12
  • 16
0

Instead of fputcsv(...) you could use implode(";",$array); and fputs(...)

But remember the csv php functions follow a certain RC standard which i can't google now. I will edit the answer later. So you are not following the standard any more.

Mruf
  • 766
  • 7
  • 16