Write a comma in a CSV File with PHP

前端 未结 2 660
不知归路
不知归路 2021-01-05 16:45

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 beca

相关标签:
2条回答
  • 2021-01-05 17:00

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

    0 讨论(0)
  • 2021-01-05 17:19

    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 讨论(0)
提交回复
热议问题