php按行按长度读取txt数据并存储到mysql
58131 2012 041181603358--------------- 278 4---------------------------------------------------------------------------------------------------------V1.00
0021 6773 6760 7201 7266 7162 7393 7427 7574 7440 7303 7321 7440 6927 7233 7219 7504 7555 7458 7518 7833 7852 7652 7040 6672 6699 5926 5717 5738 6073 6056 6325 6352 6965 7062 7094 7278 7465 7292 7324 7475 7562 7546 7314 7821 7869 7814 8178 8020 8300 7930 8608 8833 8643 8910 8927 8857 8976 8798 9535 9009
0022 9028 9818 9304 9042 8616 9044 9664 9966 970810022 962010491 9878 955810206100981000010564 99021020910279 8850 9246 9647 9310 8204 7860 8477 8313 8029 6600 6945 6503 6377 6212 5610 5732 5321 5302 5530 5973 5057 4293 4014 4826 5012 4955 4643 5218 5708 5861 6365 6756 6637 6534 8180 6732 6103 7344 7051
$fn = 'V5813101.012';
$ar = file($fn, FILE_IGNORE_NEW_LINES
[解决办法]
FILE_IGNORE_NEW_LINES);
//处理首行
$h = sscanf(str_replace(' ', '_', $ar[0]), "%5s%5s%5s%5s%5s%15s%5s%5s%249s%5s");
array_walk($h, function(&$v, $k, $t) { $v = strtr($v, $t); }, array('_' => ' '));
print_r($h);
//处理剩余行
for($i=1; $i<count($ar); $i++) {
$t = str_split(' '.$ar[$i], 5);
$p = str_split(trim(array_shift($t)), 2);
$t = array_merge($p, $t);
print_r($t);
}
array_walk($h, 'foo', array('_' => ' '));
function foo(&$v, $k, $t) { $v = strtr($v, $t); }