I need to highlight a keyword in a paragraph, as google does in its search results. Let’s assume that I have a MySQL db with blog posts. When a user searches for a certain keyword I wish to return the posts which contain those keywords, but to show only parts of the posts (the paragraph which contain the searched keyword) and to highlight those keywords.
My plan is this:
- find the post id which has the searched keyword in it’s content;
- read the content of that post again and put each word in a fixed buffer array (50 words) until I find the keyword.
Can you help me with some logic, or at least to tell my if my logic is ok? I’m in a PHP learning stage.
If it contains html (note that this is a pretty robust solution):
Results in:
And with:
You get (broken onto multiple lines for readability):
Beware of non-dom solutions (like
regexorstr_replace) since highlighting something like “div” has a tendency of completely destroying your HTML… This will only ever “highlight” strings in the body, never inside of a tag…Edit Since you want Google style results, here’s one way of doing it:
Ok, so what that does is return an array of matches with
$maxStubSizewords around it (namely up to half that number before, and half after)…So, given a string:
Calling
getKeywordStubs($string, array('bar', 'bunch'))will result in:So, then you could build your result blurb by sorting the list by
strlenand then picking the two longest matches… (assuming php 5.3+):Which results in:
I hope that helps… (I do feel this is a bit… bloated… I’m sure there are better ways to do this, but here’s one way)…