# test string my $link_results = '
Hello: World Hello hello drewboy Drewboy: Drewboy!
'; # define an array of the terms we want to bold my @terms = ( 'Hello:', 'Drewboy!' ); # make all the terms regex safe by quotemeta-ing them $_ = quotemeta $_ for @terms; # join all terms with a pipe | so we find any of them - alternation my $bold = join '|', @terms; # make all the subs - case sensitive and global $link_results =~ s#(<[^>]+?>)|($bold)#$1 ? $1 : "$2"#eg; # proof is in da pudding print $link_results; ####
package Filter;
use strict;
use base 'HTML::Parser';
my ($filter, $sub_OK);
my @ok_tags = qw ( h1 h2 h3 h4 p );
my %ok_tags;
$ok_tags{$_}++ for @ok_tags;
my @terms = ( 'head', 'Parser' );
$_ = quotemeta $_ for @terms;
my $bold = join '|', @terms;
sub start {
my ($self, $tag, $attr, $attrseq, $origtext) = @_;
$sub_OK = exists $ok_tags{$tag} ? 1 : 0;
$filter .= $origtext;
}
sub text {
my ($self, $text) = @_;
$text =~ s#\b($bold)\b#$1#g if $sub_OK;
$filter .= $text;
}
sub comment {
# uncomment to not strip comments
# my ($self, $comment) = @_;
# $filter .= "";
}
sub end {
my ($self, $tag, $origtext) = @_;
$filter .= $origtext;
}
my $parser = new Filter;
my $html = join '', ;
$parser->parse($html);
$parser->eof;
print $html;
print "\n\n------------------------\n\n";
print $filter;
__DATA__
Title
Hello Parser
You need HTML::Parser to ger ahead
So use your head
Parser rocks my head!
html.head.parser.com
use HTML::Parser;
head