Satch3000
Satch3000

Reputation: 49422

PHP Save output to csv file

I have a long list echoed on my screen looks like this:

UVVI0287;PMS340C
UVVI0288;PMS242C

etc

Here's the code:

foreach(glob('./xls/*.*') as $filename){

     $final = preg_replace('%^([^ ]+?)( )(.*)$%', '\1;\3', $bodytag);

     echo $final;

     echo '<br>';

 }

Is there a way of saving this output to csv using php?

Upvotes: 0

Views: 81

Answers (3)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

Do something like this...

$fp = fopen('file.csv', 'w');

foreach(glob('./xls/*.*') as $filename){
     $final = preg_replace('%^([^ ]+?)( )(.*)$%', '\1;\3', $bodytag);
     $temp = explode(';',$final);
     fputcsv($fp, $temp);
 }

fclose($fp);

Look into fputcsv.

Upvotes: 1

Rimble
Rimble

Reputation: 893

file_put_contents('output.csv', $final);

I'm not really sure as to what exactly you are trying to output. But I might be able to update the answer based on your reply.

Upvotes: 0

dkasipovic
dkasipovic

Reputation: 6130

Something like this?

$final = "";
foreach(glob('./xls/*.*') as $filename){
  $final .= preg_replace('%^([^ ]+?)( )(.*)$%', '\1;\3', $bodytag)."\r\n";
}
file_put_contents("final.csv",$final);

Upvotes: 0

Related Questions