Skip to content

Instantly share code, notes, and snippets.

@ternavsky
Last active August 29, 2015 14:22
Show Gist options
  • Save ternavsky/da3a2334b7aac2608ec6 to your computer and use it in GitHub Desktop.
Save ternavsky/da3a2334b7aac2608ec6 to your computer and use it in GitHub Desktop.
Converts array to csv string
public static 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()...
foreach ($input as $fields) {
fputcsv($fp, $fields, $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);
// ... close the "file"...
fclose($fp);
// ... and return the $data to the caller, with the trailing newline from fgets() removed.
return rtrim($data, "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment