samedi 1 août 2015

highlight text with surrounding words

i want to highlight text in a given string with given keywords and add a random number of surrounding words.

Example sentence:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed.

Example keyword:

dolore magna

Desired result: (mark 0-4 words before and after the keyword

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et **dolore magna** aliquyam erat, sed.

What did i try?

( [\w,\.-\?]+){0,5} ".$myKeyword." (.+ ){2,5} and ([a-zA-Z,. ]+){1,3} ".$n." ([a-zA-Z,. ]+){1,3}

Any ideas how to improve this and make it more robust?

Aucun commentaire:

Enregistrer un commentaire