A function that selects a word and extracts text next to it

I have a text like:

Etiam porta sem malesuada magna mollis euismod. The integer value of ante venenatis dapibus posuere velit aliquet. Etiam porta sem malesuada magna mollis euismod. Ain-hey-leo-cam. 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 phuera 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 ate venenatis dapibus posuere velit aliquet.

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

This text appears as a description when performing a search on my website, and I want to highlight the word / line and extract the text next to it.

Not sure if the best way is php (maybe there is) or just use the jquery function. I know how to highlight a word in jQuery, but I don't know how to extract text.

In this example, I want to show as a description, because my search string is "consectetur purus" with the following text.

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

As you can see, I not only show the highlighted word, but also the text before and after it. What would be the best way to do this?

I use the swish-e library as a search engine on my website and my knoledge cgi is null, so I prefer to try the php path.

Many thanks for your help

+6
source share
3 answers

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 ...

+5
source

It depends on how you get the answer after the search query and how the search engine works.

If you are focused on searching by “typing”, for example, “Google”, then it would be better to do this with php in order to reduce transmitted data and spend less traffic.

Otherwise, if you get all the text as an answer and don’t miss the download, do it using JS using RegExp or by counting string.charAt( int ) .

+1
source

We created a small library to do this. It will highlight the words you mentioned and give you expression of sentences around your query words.

 $lorumIpsum = "three paragraps of lrum ipsum"; TextSnippet::createSnippet('Lorem', $lorumIpsum); 

This will result in the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit .... Etiam bibendum lorem nec tempus sollicitudin .... Sed in dapibus lorem .... Nunc turpis ipsum, bibendum quis sodales sed, ullamcorper et lorem . Donec et metus hendrerit, interdum elit ut, dignissim dui.

The als class supports customizing the html tag to highlight and set the minimum and maximum fopr words for the fragment.

See swisnl / textsnippet on Github.

0
source

Source: https://habr.com/ru/post/900097/


All Articles