6

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 sending it as a header.

2 Answers 2

14

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

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

1 Comment

When adding quotes around a field you'll also have to remember to escape quotes in the field! Usr str_replace for that.
9

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:

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.