note 56827 added to function.fputcsv

From: Date: Fri, 16 Sep 2005 02:47:02 +0000
Subject: note 56827 added to function.fputcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-95259@lists.php.net to get a copy of this message
Here is an adaption of the above code that adds support for double quotes inside a field. (One double quote is replaced with a pair of double quotes per the CSV format). <?php function fputcsv($filePointer,$dataArray,$delimiter,$enclosure) { // Write a line to a file // $filePointer = the file resource to write to // $dataArray = the data to write out // $delimeter = the field separator // Build the string $string = ""; // No leading delimiter $writeDelimiter = FALSE; foreach($dataArray as $dataElement) { // Replaces a double quote with two double quotes $dataElement=str_replace("\"", "\"\"", $dataElement); // Adds a delimiter before each field (except the first) if($writeDelimiter) $string .= $delimiter; // Encloses each field with $enclosure and adds it to the string $string .= $enclosure . $dataElement . $enclosure; // Delimiters are used every time except the first. $writeDelimiter = TRUE; } // end foreach($dataArray as $dataElement) // Append new line $string .= "\n"; // Write the string to the file fwrite($filePointer,$string); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fputcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56827 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56827&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56827&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95259) next »