Search code examples
phpregextext-extractiontext-parsing

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.


Solution

  • 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