Using regex in php (you can do the same in JavaScript, though).
$regex = '/([A-Za-z0-9.,-]+\s*){0,5}\sconsectetur purus(\s|[,.!?])(\s*[A-Za-z0-9.,-]+){0,5}/'; preg_match($regex, $content, $matches); echo $matches[0];
This corresponds to 0-5 words constructed using alphanumeric characters or one of .,- (finetune this), followed by the words you are looking for (with or without punctuation at the end), and then 0-5 words. This will never break a word in the middle, because a certain number of characters is reached.
Conclusion:
posuere velit aliquet.
Cras mattis consectetur purus sit amet fermentum. Etiam porta p>
Now you can finish it, for example. delete \n , add ... , highlight consectetur purus ...
source share