Reputation: 49422
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
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
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
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