mahoney
mahoney

Reputation: 98

Get ALL-CAPS word and time expression from predictably formatted string

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.

Upvotes: -1

Views: 69

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

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

Upvotes: 0

Fluff
Fluff

Reputation: 548

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

Upvotes: 1

Related Questions