Skip to content

Instantly share code, notes, and snippets.

@pmatseykanets
Forked from johanmeiring/gist:2894568
Last active August 29, 2015 14:22
Show Gist options
  • Save pmatseykanets/2bed81c01afa90c6e48c to your computer and use it in GitHub Desktop.
Save pmatseykanets/2bed81c01afa90c6e48c to your computer and use it in GitHub Desktop.
Format line as CSV
<?php
/* Forked from https://gist.github.com/johanmeiring/2894568 */
if (! function_exists('str_putcsv'))
{
function str_putcsv($input, $delimiter = ',', $enclosure = '"')
{
// Open a memory "file" for read/write
$memory = fopen('php://temp', 'r+');
// Write the $input array to the "file" using fputcsv()
$length = fputcsv($memory, $input, $delimiter, $enclosure);
// Rewind the "file" so we can read what we just wrote
rewind($memory);
// Read the entire line, except for a new line charater into a variable
$csv = fread($memory, $length-1);
// Close the "file"
fclose($memory);
// Return the formatted string
return $csv;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment