从可预测格式的字符串中获取全大写单词和时间表达式

发布于 2024-12-15 11:57:53 字数 239 浏览 3 评论 0原文

我需要一些帮助来从以下字符串中提取 2 条信息:

viewed MUI Slideshow (00:01:45)

我需要 MUI 和时间,不带括号。 viewedslideshow 将始终存在。大写字母单词的长度可以是 2 到 6 个字母。时间始终采用相同的格式。

提取的时间将针对每个大写单词进行总计和平均。

I need some help with extracting 2 pieces of information from the following string:

viewed MUI slideshow (00:01:45)

I need the MUI and the time, without the parentheses. viewed and slideshow will always be present. The capital-lettered word can be anywhere from 2 to 6 letters in length. The time is always in the same format.

The extracted time will be totalled up and averaged for each of the capitalized words.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-12-22 11:57:53

以下片段为您提供了您想要的内容:

$str = 'viewed MUI slideshow (00:01:45)';
$r = '/viewed\ ([A-Z]{2,6})\ slideshow\ \((\d{2}:\d{2}:\d{2})\)/';

if(preg_match($r, $str, $match)) {
   // Do something
   // $match[1] = MUI
   // $match[2] = 00:01:45
}

PHP PCRE手册

The following sniplet gives you what you want:

$str = 'viewed MUI slideshow (00:01:45)';
$r = '/viewed\ ([A-Z]{2,6})\ slideshow\ \((\d{2}:\d{2}:\d{2})\)/';

if(preg_match($r, $str, $match)) {
   // Do something
   // $match[1] = MUI
   // $match[2] = 00:01:45
}

PHP Manual on PCRE

ら栖息 2024-12-22 11:57:53

我使用 sscanf() 解析文本块中的多个条目,然后计算每组的总时间和平均时间。如果您正在处理文件,则可以使用fscanf()

代码:(演示)

$logs = <<<LOG
viewed MUI slideshow (00:01:45)
viewed ABC slideshow (00:03:05)
viewed MUI slideshow (00:02:28)
viewed MUI slideshow (00:10:09)
LOG;

$result = [];
foreach (explode("\n", $logs) as $line) {
    sscanf(
        $line,
        'viewed %[A-Z] slideshow (%d:%d:%d)',
        $acronym,
        $hours,
        $minutes,
        $seconds
    );
    $result[$acronym]['total'] = ($result[$acronym]['total'] ?? 0)
        + $seconds
        + ($minutes * 60)
        + ($hours * 3600);
    $result[$acronym]['count'] = ($result[$acronym]['count'] ?? 0) + 1;
}
var_export(
    array_map(
        fn($row) => [
            'totalTime' => gmdate('H:i:s', $row['total']),
            'avgTime' => gmdate('H:i:s', intdiv($row['total'], $row['count']))
        ],
        $result
    )
);

输出:

array (
  'MUI' => 
  array (
    'totalTime' => '00:14:22',
    'avgTime' => '00:04:47',
  ),
  'ABC' => 
  array (
    'totalTime' => '00:03:05',
    'avgTime' => '00:03:05',
  ),
)

您需要将 gmdate() 替换为如果您的总时间可能超过 1 天,则采用更精细的时间格式设置方法。请参阅将秒转换为小时:分钟:秒

I've used sscanf() to parse multiple entries in a block of text, then calculated the total and average time for each group. If you are processing a file, you might use fscanf().

Code: (Demo)

$logs = <<<LOG
viewed MUI slideshow (00:01:45)
viewed ABC slideshow (00:03:05)
viewed MUI slideshow (00:02:28)
viewed MUI slideshow (00:10:09)
LOG;

$result = [];
foreach (explode("\n", $logs) as $line) {
    sscanf(
        $line,
        'viewed %[A-Z] slideshow (%d:%d:%d)',
        $acronym,
        $hours,
        $minutes,
        $seconds
    );
    $result[$acronym]['total'] = ($result[$acronym]['total'] ?? 0)
        + $seconds
        + ($minutes * 60)
        + ($hours * 3600);
    $result[$acronym]['count'] = ($result[$acronym]['count'] ?? 0) + 1;
}
var_export(
    array_map(
        fn($row) => [
            'totalTime' => gmdate('H:i:s', $row['total']),
            'avgTime' => gmdate('H:i:s', intdiv($row['total'], $row['count']))
        ],
        $result
    )
);

Output:

array (
  'MUI' => 
  array (
    'totalTime' => '00:14:22',
    'avgTime' => '00:04:47',
  ),
  'ABC' => 
  array (
    'totalTime' => '00:03:05',
    'avgTime' => '00:03:05',
  ),
)

You will need to replace gmdate() with a more elaborate time formatting approach if your total times might exceed 1 day. See Convert seconds to Hour:Minute:Second

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文