Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active December 12, 2015 08:59
Show Gist options
  • Save satooshi/4748545 to your computer and use it in GitHub Desktop.
Save satooshi/4748545 to your computer and use it in GitHub Desktop.
LTSV line parser for php.
#!/usr/bin/env php
<?php
while (false !== $line = fgets(STDIN)) {
print_r(parse($line));
}
// 68 sec
function parse($line)
{
$hash = array();
array_walk(
explode("\t", $line),
function ($f) use (&$hash) {
list($l,$v) = explode(":", $f, 2);
$hash[$l] = $v;
}
);
return $hash;
}
// 51 sec
function parse2($line)
{
$hash = array();
$fields = explode("\t", $line);
foreach ($fields as $f) {
list($l,$v) = explode(":", $f, 2);
$hash[$l] = $v;
}
return $hash;
}
//48 sec
function parse3($line)
{
$hash = array();
foreach (explode("\t", $line) as $f) {
$h = explode(":", $f, 2);
$hash[$h[0]] = $h[1];
}
return $hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment