Displaying search results "Google like"
Working in ASP.NET (VB) I am trying to create a simple search results page for my site.
The process looks like this:
-
The site user enters a search phrase;
-
The search results page searches the site's database, returns the page title as a link, and a short snippet from each search hit with a highlighted search phrase.
I already have the search part as well as the highlighted part (using Regex). However, I want to be able to return a short piece of text that includes the search phrase (a few words before the search phrase, a few after). Sort of:
Page title [as a link]
... yada yada yada search phrase yada yada yada ....
+2
a source to share
2 answers
Google displays the offer in which the keyword was found. Assuming you already found the keyword in the text, I would do:
- Backtrack char by char from the keyword position until you find
.
either?
or!
or the beginning of the text. - Returns a substring of the required length from this position.
+1
a source to share