37
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでfputcsv使いたいけど一時ファイル作りたくない時のアレ

Last updated at Posted at 2013-08-17

 毎回良く忘れて、毎回ググる。

 要は内部バッファ取ってから標準出力に吐き出してって感じ。

//generate csv
ob_start();

foreach ($csv as $fields)
{
    fputcsv(fopen('php://output', 'w'), $fields);
}
$str = ob_get_clean();
$str = mb_convert_encoding($str,"sjis","utf8");
return $str;

まぁCSVと来たらExcelなので基本はSJISエンコード。ob_周りの使い方もいっつも忘れるんよねぇ。消すとか返すとかいろいろややこしくて…

追記(08/19):色々とツッコミを頂きました。なんでもSJISってのはまずいですね…コメント欄参照してください。

37
35
2

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
37
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?