Search results with excerpt

I am implementing a search and would like to include an excerpt of the text surrounding the query. As far as I have gathered by searching the forum, I would have to write my own function/plugin for this, but as I’m not very proficient at PHP, I am having some trouble.

I found this snippet by @texnixe which looks like what I would need. However, I am searching all fields and not just text fields, which is probably why this is not working for me.

I am using the basic cookbook code:

site/templates/search.php

<form>
      <input type="search" name="q" value="<?= html($query) ?>">
      <input type="submit" value="Search">
</form>

<ul>
      <?php foreach ($results as $result): ?>
      <li>
            <a href="<?= $result->url() ?>">
                  <?= $result->title() ?>
            </a>
      </li>
      <?php endforeach; ?>
</ul>

site/controllers/search.php

<?php

return function ($site) {
	$query = get('q');
	$results = $site->search($query, ['words' => true]);

	return [
		'query' => $query,
		'results' => $results,
	];
};

Any help would be greatly appreciated!

… and in case there is no solution to this when filtering through all field types, I would still be very grateful for a more detailed explanation of how exactly to implement @textnixe’s method that I mentioned. I tried registering it as a Pages method but to no avail. I am copying it here for clarity:

/**
 * 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;
}

Thanks a lot in advance!

This is a function that should go into a plugin’s index.php. Then you can use it whereever you need it.

Alright, I created the folder site/plugins/searchexcerpt and copied the code into an index.php. Sorry for being stupid, but then how exactly would I use it…? I tried this, which doesn’t return anything for $text:

<?php foreach ($results as $result): ?>
      <li>
            <a href="<?= $result->url() ?>">
                  <?= $result->title() ?><br>
                  <?= $text ?>
            </a>
      </li>
<?php endforeach; ?>

Example usage

 <?php foreach ($results as $result) : ?>
          <li class="search-result | -mb-large">
            <h2><a href="<?= $result->url() ?>"><?= $result->title() ?></a></h2>
            <?= getResultText($result->text(), $query) ?>
          </li>
        <?php endforeach ?>

Where $query is the original query

Awesome, thanks so much! I had to modify your solution a little to fit with my field names, so for anybody else who isn’t getting a result with the above answer, try this:

<?= getResultText($result->description(), $query) ?>
<?= getResultText($result->someotherfield(), $query) ?>
etc.