i want to highlight the keyword searched , i found the solution here but it is showing whole string with matching word highlighted I want only 10word after and before of the match This is what i am using but does not work
$text = preg_replace("/^.*?(.{0,1})\b($keyword)\b(.{0,1}).*?$/i", '\1<span class="highlight">\2</span>\3', $text);
echo $text ;
Now tested with php. Something like ...
<?php
$src = 'twelve eleven ten nine eight seven six five four three two one KEYWORD one two three four five six seven eitht nine ten eleven twelve';
$keyword = "KEYWORD";
$regex = "/(.*?)((\w+\W+){0,10})($keyword\W+)((\w+\W+){0,10})(.*)/i";
$text = preg_replace("$regex", '\1<span class="highlight">\2\4\5</span>\7', $src);
echo $text;
?>
... should do it (highlighting the keyword plus 10 words before and after. Probably there are more compact alternatives...
Sample input:
twelve eleven ten nine eight seven six five four three two one KEYWORD one two three four five six seven eitht nine ten eleven twelve
... outputs ...
twelve eleven <span class="highlight">ten nine eight seven six five four three two one KEYWORD one two three four five six seven eitht nine ten </span>eleven twelve