I am developing a search facility for my website. Once all or part of the word is entered into the search, I want it to find matches and then display them.
At the moment if it finds a whole word from the DB it will highlight it but not part of the word e.g.
Word in DB = "backpack"
Search = "backpack"
Highlighted = "backpack"
Word in DB = "backpack"
Search = "back"
Highlighted = "" //nothing as apposed to what I need
I use this function where the parameters $text is the string to search through and $words is the search term:
function highlightWords($text, $words)
{
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** quote the text for regex ***/
$word = preg_quote($word);
/*** highlight the words ***/
$text = preg_replace("/\b($word)\b/i", '<span class="highlight_word">\1</span>', $text);
}
/*** return the text ***/
return $text;
}
I tried to do a preg_match but to no avail - would this be correct for what im looking for?
Cheers anyone for any suggestions