LoginSignup
1
2

More than 5 years have passed since last update.

CSVをPHPから出力する方法

Last updated at Posted at 2019-01-25


<?php

$data = array('hoge,fuga', "hoge\nfuga");
$csv = '';
$filename = 'test.csv';

foreach ($data as $key => $value) {
    //カンマ対応
    $value = str_replace(',', '","', $value);
    //改行対応
    $value = str_replace("\n", chr(10), $value);
    $csv .=  $value . ',';
    $csv .=  "\n"; 
}

    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=$filename");
    echo mb_convert_encoding($csv,"SJIS", "UTF-8");

がそれっぽいので
実装してみるっためメモ失礼します。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2