With the search function from the cookbook, I only ever see a defined text area (e.g. headline, subheadline) in the results. However, I cannot see whether the article matches my search. A solution like the one in the screenshot would be nice. Is there already a plugin for this, or a solution that is not too complex?
texnixe
December 27, 2023, 8:34pm
2
Might be useful as a starting point:
In the example where I used it at the time, my search was limited to title and text field. And I limited highlighting to the text field.
The code snippet is a function that can go into the controller or into a plugin index.php, then has to be called on the text field:
<?php foreach ($results as $result) : ?>
<li>
<h2><a href="<?= $result->url() ?>"><?= $result->title() ?></a></h2>
<?= getResultText($result->text(), $query) ?>
</li>
<?php endforeach ?>
If you search through all po…
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…
bnomei
January 3, 2024, 10:45am
3
i can recommend this js lib
1 Like
Thanks for the tip! That looks like a very mature solution
… which unfortunately hasn’t been maintained for 6 years