0

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

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

Comments

0
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.

Comments

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.