php export csv with quotes in it

Discussion in 'PHP' started by xbat, May 18, 2015.

  1. #1
    my first page where I need to have quotes and commas in it... Any help is appreciated.

    // this will not work \/ I just need to know how to have commas and quotes in this.
    $csv_output .= ("<sdfad"test>"). ",";




    my second page


    /*
    This file will generate our CSV table. There is nothing to display on this page, it is simply used
    to generate our CSV file and then exit. That way we won't be re-directed after pressing the export
    to CSV button on the previous page.
    */
    //First we'll generate an output variable called out. It'll have all of our text for the CSV file.
    $out = '';
    //Next let's initialize a variable for our filename prefix (optional).
    $filename_prefix = 'csv';
    //Next we'll check to see if our variables posted and if they did we'll simply append them to out.
    if (isset($_POST['csv_hdr'])) {
    $out .= $_POST['csv_hdr'];
    $out .= "\n";
    }
    if (isset($_POST['csv_output'])) {
    $out .= $_POST['csv_output'];
    }
    //Now we're ready to create a file. This method generates a filename based on the current date & time.
    $filename = $filename_prefix."_".date("Y-m-d_H-i",time());
    //Generate the CSV file header
    header("Content-type: application/vnd.ms-excel");
    header("Content-Encoding: UTF-8");
    header("Content-type: text/csv; charset=UTF-8");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");
    echo "\xEF\xBB\xBF"; // UTF-8 BOM
    //Print the contents of out to the generated file.
    print $out;
    //Exit the script
    exit;
     
    Solved! View solution.
    xbat, May 18, 2015 IP
  2. #2
    You should use fputcsv, it will take care of that for you. Combine that with php standard output, you don't have to save the file.
     
    ThePHPMaster, May 18, 2015 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3
    I just ended up going with that.. I guess I was looking for a easier way.
     
    xbat, May 20, 2015 IP