Please take a look at this code (thanks to user budwiser), it matches a certain word in a text and ads a span class to it:
function highlightWord($word, $text){
return str_replace($word, '<span class="highlighted">' . $word . '</span>', $text);
}
$text = highlightWord('fitness', $text);
Any ideas how to make this code work for arrays?
I’d need something like this:
$words = array("fitness", "aerobic", "fashion");
$text = highlightWord($words, $text);
Any ideas?
Thank you!
UPDATE
$profile_rule = 'profile LIKE (\'fitness\') AND profile LIKE (\'aerobic\')';
preg_match_all('/\'(.*?)\'/',$profile_rule,$match);
Any idea why $match array does not work?
I want to extract the words fitness and aerobic from the $profile_rule and use the $match array for the function above.
Ty!
This should do the trick. Simply iterate over the words and add the highlight code.