PHP - 解析具有固定列宽的文本文件

o-r*_*rod 2 php database parsing laravel

我是 PHP 和 Laravel 的新手,我需要打开文件并解析内容以将它们传递到数据库。文本文件具有固定的列宽。它没有分隔符或标题。我认为使用子字符串并将每个子字符串分配给变量将是正确的方法,但我仍在学习该语言的过程中,我不知道如何实现这一点。

以下是文本文件中的数据示例:

1618 0002 9                    1261    4141191269    4141191269       4      002     0

1606 0000 9                    1159    4169191388    4169191388       4      012     0

1607 0009 7    9  505   04129284134          1245          1245 56984 4         

1619 0000 9    6               1172    2129922686    2129922686       4      013     0

1606 0000 9                    1159    4169191388    4169191388       4      012     0

1607 0009 7    9  505   04129284134          1245          1245 56984 4         

1619 0000 7    6  521         1188#          1172          1172       0          001 4

1606 0000 9                    1159    4169191388    4169191388       4      012     0

1607 0009 7    9  505   04129284134          1245          1245 56984 4         

1619 0000 9    6               1252    2129922686    2129922686       4      003     0

1606 0000 9                    1159    4169191388    4169191388       4      012     0

1607 0009 7    9  505   04129284134          1245          1245 56984 4         

1619 0000 7    6  521         1188#          1252          1252       1          002 4
Run Code Online (Sandbox Code Playgroud)

编辑:

列名称:

id
time
duration
cond_code
code_dial
code_used
dialed_num
calling_num
clg_num_in_tac
auth_code
frl
ixc_code
in_crt_id
out_crt_id
Run Code Online (Sandbox Code Playgroud)

小智 6

不知道为什么每个人都建议解析制表符分隔文件的方法。使用unpack()。我曾经通过手动解析文件并编写函数来提取数据来做到这一点 - \xe2\x80\x94真是浪费时间。我发现这篇文章解释了如何以干净的自记录方式做到这一点。

\n\n

$records[] = unpack(\'A40Address 1/A40Address 2/A25City/A2State/A10Zip Code\',$row);

\n