Unwanted spaces in stripped text using excerpt()

I’ve been converting a K2 project over to K3 and have noticed a little glitch in the way that links are stripped out of blocks of text when using excerpt() in K3.

I’m using markdown to add links to the text field in my content files, and have been using the following code to trim down that text when it’s displayed in a search result:


In K2, the search results look like this:

Screen Shot 2022-05-04 at 2.37.32 PM

In K3, if a markdown link is followed by a punctuation mark (such as a period or comma) and that link gets stripped from the text, an extra space gets inserted between the originla linked text and the punctuation.

In the example below, the words “cursor”, “input”. and “tapped” were all linked in the the original text field.

Screen Shot 2022-05-04 at 2.37.43 PM

Can I assume that this is a bug in the excerpt() method?

It happens because of this line:

$string = strip_tags(str_replace('<', ' <', $string));

in the Str::excerpt() method.

Ok, but should it be happening in the first place?

K2 doesn’t do this, and it’s rather annoying.

No, that shouldn’t happen. Although, I guess there was a reason this string replacement was added. Maybe create an issue on GitHub?

In the meantime, you can do a string replacement:

<?php echo str_replace(' .', '.', $page->textarea()->kt()->excerpt(140)) ?>

Or with an array of characters as first argument to include question marks, exclamation marks etc.

Thanks for the workaround example.

I was going to try a string replacement similar to this, regardless.

And I will create an issue on GitHub as well.

Issue created.

I’m pleased to report that this issue has been fixed in the Kirby 3.8 release candidate.