Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight particular words

Status
Not open for further replies.

Geronantimo

Technical User
Apr 3, 2006
79
SE
I have some web pages with dynamically generated content and I wonder if PHP can be used to highlight particular words that I choose.

There seem to be plenty of methods for highlighting search terms or for the user to choose words to highlight, but I would like to specify which words are highlighted (if they appear in the content).

I'd be grateful for being pointed in the right direction. Is it even possible?

 
Yes it's possible, look around for php highlighting, and I've seen a few, I don't have any references off hand but they do something similar to google toolbar's highlight.

-Kerry
 
you could try something like this

Code:
$words = array("apple","apples", "banana", "bananas", "cabbage", "cabbages");
$colour = "blue";
$str = "apples are green, bananas are yellow and cabbage is disgusting";

foreach ($words as $srch):
  $str = str_replace($srch, "<span style=\"color:$colour;\">$srch</span>", $str);
endforeach;
echo $str;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top