根据 PHP 的日期使用 preg_split 拆分日志文件?

Split a log file using preg_split based on dates with PHP?

我有一个这种格式的 txt 文件:

14/12/2020 12:02:50
LOG_HERE_1 XXXXX

14/12/2020 12:04:55
LOG_HERE_2 XXXXX

14/12/2020 12:10:33
LOG_HERE_3 XXXXX

我需要解析它,在日期上使用正则表达式 (dd/mm/yyyy hh:mm:ss),但将日期保留在数组中。 例如:

Array(
 [0] => '14/12/2020 12:02:50 LOG_HERE_1 XXXXX',
 [1] => '14/12/2020 12:02:50 LOG_HERE_2 XXXXX',
 [2] => '14/12/2020 12:02:50 LOG_HERE_3 XXXXX'
)

我试过这个:

$array = preg_split('/(\d{2}\/\d{2}\/\d{4}\s\d{2}[:]\d{2}[:]\d{2})/', $data, null, PREG_SPLIT_DELIM_CAPTURE);

但它告诉我:

{
 0: "",
 1: "14/12/2020 12:02:50",
 2: "",
 3: "14/12/2020 12:04:55",
 4: "",
 5: "14/12/2020 12:10:33",
 6: ""
}

使用标志 PREG_SPLIT_DELIM_CAPTURE 您还可以匹配以日期时间开头的行,如格式,后跟所有不以使用负先行 (?![=18= 的行开头的行]

^(\d{2}/\d{2}/\d{4}\h\d{2}:\d{2}:\d{2}\b.*\R(?:(?!\d{2}/\d{2}/\d{4}\h\d{2}:\d{2}:\d{2}\b).*\R?)*)

如果以类似日期的模式开头也足够,您可以将其缩短为:

^(\d{2}/\d{2}/\d{4}\b.*\R(?:(?!\d{2}/\d{2}/\d{4}\b).*\R?)*)

看到一个regex demo

例如

$pattern = "~^(\d{2}/\d{2}/\d{4}\h\d{2}:\d{2}:\d{2}\b.*\R(?:(?!\d{2}/\d{2}/\d{4}\h\d{2}:\d{2}:\d{2}\b).*\R?)*)~m";    
$result = preg_split($pattern, $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($result);

看到一个php demo or with the log on the same line.

输出

Array
(
    [0] => 14/12/2020 12:02:50
LOG_HERE_1 XXXXX


    [1] => 14/12/2020 12:04:55
LOG_HERE_2 XXXXX


    [2] => 14/12/2020 12:10:33
LOG_HERE_3 XXXXX
)

注意类似日期的模式本身不会验证日期。