Hey there,
I'm building a site search feature for our website, and I'm having a hard time. The results come from a MySQL fulltext search, which works fine. But I want PHP to select an excerpt from the body of the article so that the user knows in what context the search was made.
So, if a word is found in the body, PHP should select X words to the left and right of the word. It should account for punctuation on either side of the matched word (things like commas, exclamations, colons, semi-colons, periods and so on).
It should also be able to handle beginning and end (maybe the search word was the first or last word in the article and doesn't have X words to the left and right of it).
I've been working on a php regex pattern to do this, but it doesn't exactly do what I want. It works mostly, but misses the target in one situation.
Suppose the search word was 'love'. If the sentence read "... and he has a deep love and appreciation for the job..." it should match the word "love" (plus X words to the left and right) because love is its own word.
Another example: suppose the sentence said "... the job was true love. He knew he was at the right...". This should match the word love, even though it ends with a period.
But if the sentence read "... and he loves the job and appreciates ... " then it shouldn't match. But it does with my code.
All in all, I just want the behavior to match that of the MySQL FULLTEXT search (using MySQL 5.0.26 on SUSE Linux). I want the search excerpts to match that of what the database found.
Suggestions?