我有一个非常大的文档,我正在尝试解析信息并将其放入数据库中。信息被半组织成可管理的块,由管道分隔的数字将始终是相同的数量(在这种情况下为3)。
000000&V-1 some text
1|2|3 A form
4|5|6 B form
000000&V-2 different text
7|8|9 C form
10|11|12 D form
13|14|15 E form
16|17|18 F form
000000&V-3 more different text
19|20|21 G form
000000&V-4 some more text
22|23|24 H form
25|26|27 I form
使用PHP,我知道如何从我将要获得的数组中将数据输入数据库,我只是不知道如何解析这些部分以获得我想要的PHP。
我需要在000000&V-
之后检索数字,然后检索文本字符串,然后检索所有数字和格式字母并放入数组中,如下所示:
{1, some text, 1, A}
{1, some text, 2, A}
{1, some text, 3, A}
{1, some text, 4, B}
{1, some text, 5, B}
{1, some text, 6, B}
{2, different text, 7, C}
{2, different text, 8, C}
{2, different text, 9, C}
{2, different text, 10, D}
....ETC!
答案 0 :(得分:0)
You can use some loops alongside with preg_match and explode functions:
$firstColumn = null;
$secondColumn = null;
$separator = '/000000\&V\-(\d+)\s(.+)/';
$result = [];
foreach ($contents as $line) {
// Retrieve current first and second columns values from separator.
if (preg_match($separator, $line, $matches)) {
$firstColumn = $matches[1];
$secondColumn = $matches[2];
continue;
}
// If we here, the current line is not a separator, so get numbers
// and letter from it.
list($numbers, $letter, $_) = explode(' ', $line);
// // Cool functional style
// $result = array_merge($result, array_reduce(
// explode('|', $numbers),
// function ($carry, $number) use ($firstColumn, $secondColumn, $letter) {
// $carry[] = [$firstColumn, $secondColumn, $number, $letter];
// return $carry;
// },
// []
// ));
// "Bad" nested loops style
// Explode numbers and add record for each number into the result.
foreach (explode('|', $numbers) as $number) {
$result[] = [$firstColumn, $secondColumn, $number, $letter];
}
}
Here is the demo.