开发者

Write a comma in a CSV File with PHP

i am sending a csv file as a header and i want to use a comma (not to seperate, just to use). how would i do that? i'm using PHP and i can not use fputcsv because i'm sendi开发者_开发技巧ng it as a header.


Just use '"'.$value.'"' around it and it will be fine.


Either write the CSV to file first and then send it via readfile or write it to the output stream directly:

fputcsv(
    fopen('php://output', 'w+'),
    array(
        'Some text',
        'Some text with commas ,,,,,',
        'Other text'
    )
);

which will then print

"Some text","Some text with commas ,,,,,","Other text"

Note that fputcsv will also allow you to change delimiters and enclosures, so in addition to just wrapping the value in quotes you can also just change the delimiter to, for instance, a semicolon.

See the PHP Manual on supported wrappers in fopen:

  • http://php.net/manual/en/wrappers.php.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜