in reply to Text highlighting (not language syntax)
use HTML::Entities qw( encode_entities ); sub text_to_html { my $terms = $_[1]; my ($terms_pat) = map "(?:$_)", join '|', map quotemeta, @$terms; ( my $html = $_[0] ) =~ s{ ( (?: (?!$terms_pat). )* ) ( $terms_pat+ ) | ( .+ ) }{ defined($1) ? encode_entities("$1") . "<b>" . encode_entities("$2") . "</b>" : encode_entities("$3") }xseg; return $html; } print text_to_html("best car wash & repair!!", [qw( car repair )]);
Starting from HTML is much trickier.
Update: Simplified code.
Update: Fixed s// </b> / problem mentioned in a reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Text highlighting (not language syntax)
by vit (Friar) on Jan 22, 2010 at 23:09 UTC | |
by ikegami (Patriarch) on Jan 22, 2010 at 23:09 UTC | |
by vit (Friar) on Jan 22, 2010 at 23:25 UTC |