note 90883 added to function.fputcsv

From: Date: Thu, 14 May 2009 15:17:01 +0000
Subject: note 90883 added to function.fputcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-154595@lists.php.net to get a copy of this message
I've created a function for quickly generating CSV files that work with Microsoft applications. In the field I learned a few things about generating CSVs that are not always obvious. First, since PHP is generally *nix-based, it makes sense that the line endings are always \n instead of \r\n. However, certain Microsoft programs (I'm looking at you, Access 97), will fail to recognize the CSV properly unless each line ends with \r\n. So this function changes the line endings accordingly. Secondly, if the first column heading / value of the CSV file begins with uppercase ID, certain Microsoft programs (ahem, Excel 2007) will interpret the file as being in the SYLK format rather than CSV, as described here: http://support.microsoft.com/kb/323626 This function accommodates for that as well, by forcibly enclosing that first value in quotes (when this doesn't occur automatically). It would be fairly simple to modify this function to use another delimiter if need be and I leave that as an exercise to the reader. So quite simply, this function is used for outputting CSV data to a CSV file in a way that is safe for use with Windows applications. It takes two parameters + one optional parameter: the location of where the file should be saved, an array of data rows, and an optional array of column headings. (Technically you could omit the headings array and just include it as the first row of the data, but it is often useful to keep this data stored in different arrays in practice.) <?php function mssafe_csv($filepath, $data, $header = array()) { if ( $fp = fopen($filepath, 'w') ) { $show_header = true; if ( empty($header) ) { $show_header = false; reset($data); $line = current($data); if ( !empty($line) ) { reset($line); $first = current($line); if ( substr($first, 0, 2) == 'ID' && !preg_match('/["\\s]/', $first) ) { array_shift($data); array_shift($line); if ( empty($line) ) { fwrite($fp, "\"{$first}\"\r\n"); } else { fwrite($fp, "\"{$first}\","); fputcsv($fp, $line); fseek($fp, -1, SEEK_CUR); fwrite($fp, "\r\n"); } } } } else { reset($header); $first = current($header); if ( substr($first, 0, 2) == 'ID' && !preg_match('/["\\s]/', $first) ) { array_shift($header); if ( empty($header) ) { $show_header = false; fwrite($fp, "\"{$first}\"\r\n"); } else { fwrite($fp, "\"{$first}\","); } } } if ( $show_header ) { fputcsv($fp, $header); fseek($fp, -1, SEEK_CUR); fwrite($fp, "\r\n"); } foreach ( $data as $line ) { fputcsv($fp, $line); fseek($fp, -1, SEEK_CUR); fwrite($fp, "\r\n"); } fclose($fp); } else { return false; } return true; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 66.170.4.122 ---- Manual Page -- http://www.php.net/manual/en/function.fputcsv.php Edit -- https://master.php.net/note/edit/90883 Del: integrated -- https://master.php.net/note/delete/90883/integrated Del: useless -- https://master.php.net/note/delete/90883/useless Del: bad code -- https://master.php.net/note/delete/90883/bad+code Del: spam -- https://master.php.net/note/delete/90883/spam Del: non-english -- https://master.php.net/note/delete/90883/non-english Del: in docs -- https://master.php.net/note/delete/90883/in+docs Del: other reasons-- https://master.php.net/note/delete/90883 Reject -- https://master.php.net/note/reject/90883 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154595) next »