Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Convert a simple CSV File with PHP, list of different possible ways how to reformat a simple csv
<?php
$start = microtime(true);
$fpIn = fopen('input.csv', 'r');
$fpOut = fopen('output-pure.csv', 'w');
while (($row = fgets($fpIn)) !== false)
{
$fields = explode(";", $row);
fwrite($fpOut,
implode(
";",
array(
"name" => $fields[0],
"firstname name" => '"'.$fields[1] ." ". $fields[0].'"',
"kto" => $fields[2],
"blz" => $fields[3],
"amount" => $fields[4]
)
)
);
}
$end = microtime(true);
$duration = $end - $start;
echo "Duration: ".round($duration, 2) . "s".PHP_EOL;
<?php
$start = microtime(true);
$fpIn = fopen('input.csv', 'r');
$fpOut = fopen('output.csv', 'w');
while (($row = fgetcsv($fpIn, 0, ";")) !== false)
{
fputcsv($fpOut, array(
"name" => $row[0],
"firstname name" => $row[1] ." ". $row[0],
"kto" => $row[2],
"blz" => $row[3],
"amount" => $row[4]
), ";");
}
$end = microtime(true);
$duration = $end - $start;
echo "Duration: ".round($duration, 2) . "s".PHP_EOL;
<?php
// reformat the csv file via a filter
$start = microtime(true);
$fpIn = fopen('input.csv', 'r');
$fpOut = fopen('output.csv', 'w');
stream_filter_register("csvreformat", "csvreformat_filter");
stream_filter_append($fpIn, "csvreformat");
stream_copy_to_stream($fpIn, $fpOut);
$end = microtime(true);
$duration = $end - $start;
echo "Duration: ".round($duration, 2) . "s".PHP_EOL;
class csvreformat_filter extends php_user_filter {
private $remaining = "";
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in))
{
$data = $this->remaining.$bucket->data;
$lastNewLine = strrpos($data, PHP_EOL);
$this->remaining = substr($data, $lastNewLine+1);
$data = substr($data, 0, $lastNewLine);
$bucket->data = "";
foreach(explode(PHP_EOL, $data) as $row)
{
$tmp = explode(";", $row);
$bucket->data .= $tmp[0].';"'.$tmp[1]." ".$tmp[0].'";'.$tmp[2].";".$tmp[3].";".$tmp[4].PHP_EOL;
}
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
<?php
$start = microtime(true);
$fp = fopen('input.csv', 'w');
for ($i = 1; $i <= 1000000; $i++)
{
$row = array(
"name" => someString(),
"firstname" => someString(),
"kto" => rand(1000000, 9999999),
"blz" => rand(1000000, 9999999),
"amount" => rand(1000000, 9999999)
);
fputcsv($fp, $row, ";");
}
$end = microtime(true);
$duration = $end - $start;
echo "Duration: ".round($duration, 2) . "s".PHP_EOL;
function someString($length = 10) {
return substr(md5(rand()), 0, $length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment