gpt4 book ai didi

php - 在 PHP 中解析未知的字符串长度?

转载 作者:行者123 更新时间:2023-12-04 06:05:53 26 4
gpt4 key购买 nike

我从日志文件中获得了以下摘录,并且必须抓取“NC:”和“VL #”或“迄今为止的总违规级别#”之间的所有内容。鉴于长度未知并且没有简单的分隔符,我对如何获取该字符串有任何想法吗?

11.11.27 14:50:41 [WARNING] NC: elvefromhell failed fight.noswing: Didn't swing arm. VL 3

11.11.27 14:50:54 [INFO] NC: Jackmac19 failed fight.direction: tried to attack out of sight entity. Total violation level so far 5.

11.11.27 14:51:02 [WARNING] NC: Bugs6968 failed chat.spam: Last sent message "t". VL 1

11.11.27 14:51:26 [WARNING] NC: Convinced failed chat.spam: Last sent message "hi pherce". VL 1

11.11.27 14:51:48 [WARNING] NC: UmbraSprite failed blockbreak.noswing: Didn't swing arm. VL 2

11.11.27 14:51:59 [INFO] NC: AwayDood failed moving.running.vertical. VL 2

11.11.27 14:52:17 [INFO] NC: AwayDood failed moving.running.vertical. VL 11

11.11.27 14:52:35 [INFO] NC: AwayDood failed moving.running.vertical. VL 11

11.11.27 14:53:06 [WARNING] NC: AwayDood failed blockbreak.noswing: Didn't swing arm. VL 4

最佳答案

使用正则表达式:

$text = '11.11.27 14:50:54 [INFO] NC: Jackmac19 failed fight.direction: tried to attack out of sight entity. Total violation level so far 5.';

preg_match('/NC: (.*)?\s(VL|Total violation level so far)/', $text, $matches);
echo $matches[1];

这将一次在一行上运行。它可以用于处理更大的日志,但一次处理一行日志似乎更合理。

See it in action .

关于php - 在 PHP 中解析未知的字符串长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8364376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com