Live Search in Kirby 3

I created this simple method a while ago:

/**
 * Used for search to highlight the relevant search text
 */
function getResultText(Kirby\Cms\Field $text, $query) 
{
    if (stripos($text, $query)) {
      $text        = strip_tags($text->kt());
      $startQ      = stripos($text, $query);
      $queryLength = strlen($query);
      $preText     = substr($text, $startQ-150, 150);
      $afterText   = substr($text, $startQ+$queryLength, 150);
      $queryText   = substr($text, stripos($text, $query), $queryLength);
      $text        = '…' . $preText . '<span class="searchterm-highlight">' . $queryText . '</span>' . $afterText . '…';
    } else {
        $text      = $text->kt()->excerpt(250);
    }
    return $text;
}

But I only used that on the text field, not the title. The purpose was to highlight the search term and to output some characters around this term.