#!/usr/bin/perl -w
use strict;
my $terms = 'tree crow black blue green stars';
my $color = '#CC0000';
my $string1= qq!\tthe black crow is sitting in the tree looking at the green stars!;
my $string2= qq!\tthe black crow is sitting in the blue tree looking at the green stars!;
print 'Terms: '.$terms.$/x2;
print 'text1:'.$/.$string1.$/;
print 'parsed1:'.$/.HTMLhighlight($terms,$string1,$color).$/;
print 'text2:'.$/.$string2.$/;
print 'parsed2:'.$/.HTMLhighlight($terms,$string2,$color).$/;
sub HTMLhighlight {
my($searchterms,$instr,$color) = @_;
my (@terms)=split(/\ /,$searchterms);
foreach my $i (0..$#terms) {
$instr =~ s/(?!<.*)($terms[$i])(?!.*>)/$1<\/b>/gi;
}
return $instr;
}
# a easier way to do the same thing =]
sub HTMLhighlight2 {
my ($searchterms,$instr,$color) = @_;
$searchterms =~ s/\s+/|/g;
$instr =~ s#($searchterms)#$1#gi;
return $instr;
}