Last active
December 12, 2015 08:59
-
-
Save satooshi/4748545 to your computer and use it in GitHub Desktop.
LTSV line parser for php.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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