Trim the search result around the search word

A random thought: put the search string into an array ( with explode). Find the array position of the highlighted string. Count back xx for the start, and forward xx for the end. Extract those array values, then implode them back into a string.

That would work for one searchresult word in the string. You’d have to loop through multiple occurrences of the searchresult word.

Say the full search result has 200 words. The searchresult word is at position 50. Extract words starting at 30 and ending at 70. Put the words back into one string. Show the string.

Might be a better way, though, but that was my first thought.