After some search and googling I found code for highlighting and limiting the search results within a text similar to this one:
$text = preg_replace("/^.*?(.{0,100})\b($word)\b(.{0,100}).*?$/mi", '\1<span class="highlight_word">\2</span>\3', $text);
Unfortunately I always get the complete $text
back even though the contents of $word
is placed within the span as intended.
My question is now how I may reduce the contents of $text
to just show 100 characters before and after the search result (contents of $word).
I also checked the regular expression in several variants using a webportal and got the desired result. Nevertheless my php code is not showing what is intended.
Any help is really appreciated as I assume there is a very stupid error on my side.