note 91170 deleted from function.str-getcsv by cmb

From: Date: Sun, 09 Feb 2020 08:20:22 +0000
Subject: note 91170 deleted from function.str-getcsv by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-214052@lists.php.net to get a copy of this message
Note Submitter: Ulf ---- As Dave's function also had the problem with only one line being returned here's a slightly changed version: <?php function str_putcsv($input, $delimiter = ',', $enclosure = '"') { // Open a memory "file" for read/write... $fp = fopen('php://temp', 'r+'); // ... write the $input array to the "file" using fputcsv()... fputcsv($fp, $input, $delimiter, $enclosure); // ... rewind the "file" so we can read what we just wrote... rewind($fp); // ... read the entire line into a variable... $data = fread($fp, 1048576); // [changed] // ... close the "file"... fclose($fp); // ... and return the $data to the caller, with the trailing newline from fgets() removed. return rtrim( $data, "\n" ); } ?> It assumes that one line won't exceed 1Mb of data. That should be more than enough.

« previous php.notes (#214052) next »