Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function that highlight a word and extract the text near it

I have a text for example :

Etiam porta sem malesuada magna mollis euismod. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Etiam porta sem malesuada magna mollis euismod. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.

Nulla vitae elit libero, a pharetra augue. Vestibulum id ligula porta felis euismod semper. Vestibulum id ligula porta felis euismod semper. Maecenas sed diam eget risus varius blandit sit amet non magna. Vestibulum id ligula porta felis euismod semper. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.

Cras mattis consectetur purus sit amet fermentum. Etiam porta sem malesuada magna mollis euismod. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Maecenas faucibus mollis interdum. Nullam quis risus eget urna mollis ornare vel eu leo.

That text appears as description on a search performed in my website and i want to be able to highlight a word/string and extract the text next to it.

Im not sure is the best way will be php(probably is) or just use a jquery function. I know how to highlight a word in jQuery but not sure how to extract the text.

In this example i want to show as description because my searched string is "consectetur purus" the following text.

....posuere velit aliquet.Cras mattis consectetur purus sit amet fermentum. Etiam porta....

As you see im not only showing the highlighted word but also some text before and after it. What would be the best method to accomplish this?

Im using the swish-e librari as search engine in my website and my knoledge of cgi is null so i prefer to try the php way.

Thanks a lot for all your help

like image 394
chifliiiii Avatar asked Oct 26 '11 00:10

chifliiiii


2 Answers

Using a regular expression in php (you could 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 matches 0-5 words built using alphanumeric chars or one of .,- (finetune this) followed by the words you're looking for (with or without punctuation at the end), followed by 0-5 words. This will never break up a word in the middle because a certain amount of chars is reached.

Output:

posuere velit aliquet.

Cras mattis consectetur purus sit amet fermentum. Etiam porta

Now you can finetune this, e.g. remove the \n, add ..., highlight consectetur purus

like image 102
middus Avatar answered Oct 21 '22 17:10

middus


It depends on how you get your answer after the searching request and the way the search engine works.

If you're target is "on typing" search, like "Google" has - then it'd be better to make it by php to cut the transferred data and waste less traffic.

Otherwise, if you get the whole text as a response and don't mind about the load - do it with JS using RegExp or counting string.charAt( int ).

like image 40
Michael Sazonov Avatar answered Oct 21 '22 18:10

Michael Sazonov